+1 vote
in Programming Languages by (56.8k points)
How can I drop some of the rows from a dataframe using row numbers?

1 Answer

+2 votes
by (351k points)
selected by
 
Best answer

The drop() function of Pandas's DataFrame can be used to delete rows or columns from a given dataframe. You need to specify axis=0 for dropping rows and indices of rows that you want to delete as a list.

Here is an example where I want to delete rows 1 and 4:

>>> import pandas as pd

>>> df=pd.DataFrame({'A':[4,6,3,1,8], 'B':[43,12,54,34,67], 'C':[32,45,12,68,43]})

>>> df

   A   B   C

0  4  43  32

1  6  12  45

2  3  54  12

3  1  34  68

4  8  67  43

>>> df.drop([1,4], axis=0)

   A   B   C

0  4  43  32

2  3  54  12

3  1  34  68

>>> 

If you want to change the original dataframe, you can specify 'inplace=True' as an additional parameter.


...