Yuki Inoue
Yuki Inoue

Reputation: 3787

Drop the first n rows from daru dataframe

One can drop the first n elements of an array by using Array#drop.

a = [1,2,3]
a.drop(2) # => [3]

I want to drop the first n rows from a Daru::DataFrame object. It seems this class does not implement such drop method.

How can I delete the first n rows from a Daru::DataFrame object?

Upvotes: 0

Views: 209

Answers (2)

simon tan
simon tan

Reputation: 44

You can put this into a loop:

df.delete_row(0)

https://www.rubydoc.info/gems/daru/0.1.4.1/Daru/DataFrame#delete_row-instance_method

Upvotes: 0

richflow
richflow

Reputation: 2144

You can use row_at to retrieve all the rows without the first 4.

Example:

2.4.5 :001 > require 'daru'
 => true
2.4.5 :002 > df = Daru::DataFrame.new({
2.4.5 :003 >         'col0' => [1,2,3,4,5,6],
2.4.5 :004 >         'col2' => ['a','b','c','d','e','f'],
2.4.5 :005 >         'col1' => [11,22,33,44,55,66]
2.4.5 :006?>       })
 => #<Daru::DataFrame(6x3)>
      col0 col2 col1
    0    1    a   11
    1    2    b   22
    2    3    c   33
    3    4    d   44
    4    5    e   55
    5    6    f   66

Retrieve rows:

2.4.5 :010 > df.row_at(4..df.shape()[0])
 => #<Daru::DataFrame(2x3)>
      col0 col2 col1
    4    5    e   55
    5    6    f   66

Upvotes: 1

Related Questions