This notebook describe indexing in Daru::DataFrame with the newly added Categorical Index and other index classes.
require 'daru'
Helper function to get a sample dataframe.
def sample_df idx
Daru::DataFrame.new({
a: 1..5,
b: 'a'..'e',
c: 11..15
}, index: idx)
end
idx = Daru::CategoricalIndex.new [:a, :b, :a, :b, :c]
df = sample_df idx
Retrive rows by category or position
Note: When index is both a valid category as well as position, then it will treated as category.
df.row[:a, :c]
df.row[0, 1]
Its to fetch vectors and works similar to #row[]
.
df[:a, :b]
To retrive rows by position.
df.row.at 0, 1, 2
To retrive vectors by position.
df.at 0, 1
Set rows by categories or positions.
Note: In case index is both a valid category and position, it will taken as category.
df.row[:a] = ['x', 'y', 'z']
df
Works similar to #row[]=
and is for vectors.
df[:a] = [1]*5
df
Set rows by positions to a given vector
#reset dataframe
df = sample_df idx
df.row.set_at [0, 4], ['x', 'y', 'z']
df
Works similar to #row.at_set
df.set_at [0, 1], [nil]*5
df