Malintha Malintha - 1 year ago 207
Python Question

Delete row after filtering in Pandas dataframe

I have dataframe(

) which contains 100000 rows. I am filtering one row from it and I used following code segment for it.

((ratings_base.loc[ratings_base['user_id'] == 1]).sort_values(by='rating', ascending=0)).iloc[0]

which returns me a

user_id 1
movie_id 170
rating 5
Name: 19996, dtype: int64

How can I delete this row(Series object) from my original dataframe object ? I can use dataframe's drop function but I need row index for it. If I can get the row index of the selected row (I can see it appears as the Name attribute of the Series object), I can drop it.

Answer Source

df.loc[0] selects the first row. df.loc[1:] selects everything after the first row.

So you could use

ratings_base = ((ratings_base.loc[ratings_base['user_id'] == 1])
                .sort_values(by='rating', ascending=0))
ratings_base = ratings_base.iloc[1:]

Alternatively, you could use df.drop:

row = ((ratings_base.loc[ratings_base['user_id'] == 1])
       .sort_values(by='rating', ascending=0)).iloc[0]
label =
ratings_base = ratings_base.drop(label)

This may be more flexible if you wish to drop a row which does not happen to be the first.

Note that ratings_base.drop will drop all rows whose index is label. So more than one row may be dropped if the index is not unique.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download