jrovegno
jrovegno

Reputation: 719

Setting With Enlargement in daru

There is some way to Setting With Enlargement in daru? Something similar to pandas with loc.

Upvotes: -1

Views: 125

Answers (1)

Sameer Deshmukh
Sameer Deshmukh

Reputation: 138

Yes you can.

For Daru::Vector objects use the #push method like so:

require 'daru'
v = Daru::Vector.new([1,2,3], index: [:a,:b,:c])
v.push(23, :r)
v
#=> 
#<Daru::Vector:74005360 @name = nil @size = 4 >
#    nil
#  a   1
#  b   2
#  c   3
#  r  23

For setting a new vector in Daru::DataFrame, call the #[]= method with your new name inside the []. You can either assign a Daru::Vector or an Array.

If you assign Daru::Vector, the data will be aligned so that the indexes of the DataFrame and Vector match.

For example,

require 'daru'
df = Daru::DataFrame.new({a: [1,2,3], b: [5,6,7]})
df[:r] = [11,22,33]
df
# => 
#<Daru::DataFrame:73956870 @name = c8a65ffe-217d-43bb-b6f8-50d2530ec053     @size = 3>
#                    a          b          r 
#         0          1          5         11 
#         1          2          6         22 
#         2          3          7         33 

You assign a row with the DataFrame#row[]= method. For example, using the previous dataframe df:

df.row[:a] = [23,35,2]
df
 #=> 
 #<Daru::DataFrame:73956870 @name = c8a65ffe-217d-43bb-b6f8-50d2530ec053  @size = 4>
 #                    a          b          r 
 #         0          1          5         11 
 #         1          2          6         22 
 #         2          3          7         33 
 #         a         23         35          2 

Assigning a Daru::Vector will align according to the names of the vectors of the Daru::DataFrame.

You can see further details in these notebooks.

Hope this answers your question.

Upvotes: 2

Related Questions