rubymultidimensional-arraydaru

Elegant way to create empty daru DataFrame with nil


I need to create an empty DataFrame with an specific shape for example [10,10] to store data from other source.

In pandas is straightforward:

pd.DataFrame(np.nan, index=range(10), columns=range(10))

But in ruby using daru, I'm not sure how to do that.

Related Question: pandas Dataframe


Solution

  • You can pass the names of the vectors in the :order option and a range as an index in the :index option. The input will be an empty Array.

    For example:

    require 'daru'
    df = Daru::DataFrame.new([], order: (1..4).to_a, index:(0..10).to_a)
    # => 
    #<Daru::DataFrame:72941700 @name = 2357edc1-f425-4ae3-aead-5a8b812ecb13 @size = 11>
    #                    1          2          3          4 
    #         0        nil        nil        nil        nil 
    #         1        nil        nil        nil        nil 
    #         2        nil        nil        nil        nil 
    #         3        nil        nil        nil        nil 
    #         4        nil        nil        nil        nil 
    #         5        nil        nil        nil        nil 
    #         6        nil        nil        nil        nil 
    #         7        nil        nil        nil        nil 
    #         8        nil        nil        nil        nil 
    #         9        nil        nil        nil        nil 
    #        10        nil        nil        nil        nil