2

I am trying to drop multiple rows from my data. I can drop rows using:

dt=dt.drop([40,41,42,43,44,45])

But I was wondering if there is a simpler way. I tried:

dt=dt.drop([40:45])

But sadly it did not work.

KonK3
  • 81
  • 1
  • 7
  • Possible duplicate of [How to drop a list of rows from Pandas dataframe?](https://stackoverflow.com/questions/14661701/how-to-drop-a-list-of-rows-from-pandas-dataframe) – Sheldore Sep 19 '18 at 21:34
  • This has already been answered here: https://stackoverflow.com/questions/14661701/how-to-drop-a-list-of-rows-from-pandas-dataframe – schaefferda Sep 19 '18 at 21:41

4 Answers4

3

I will recommend np.r_

df.drop(np.r_[40:50+1])

In case you want to drop two range at the same time

np.r_[40:50+1,1:4+1]
Out[719]: array([40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,  1,  2,  3,  4])
BENY
  • 317,841
  • 20
  • 164
  • 234
3

Assuming you want to drop a range of positions:

df.drop(df.index[40: 46])

This doesn't assume the indices are integers.

piRSquared
  • 285,575
  • 57
  • 475
  • 624
2

You can use:

dt = dt.drop(range(40,46))

or

dt.drop(range(40,46), inplace=True)
sacuL
  • 49,704
  • 8
  • 81
  • 106
0

You could generate the list based on a range:

dt=dt.drop([x for x in range(40, 46)])

Or just:

dt=dt.drop(range(40, 46))
Caleb Courtney
  • 316
  • 2
  • 5