Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas drop row based on index vs ix

Tags:

I'm trying to drop pandas dataframe row based on its index (not location).

The data frame looks like

                      DO   129518  a developer and    20066   responsible for    571     responsible for    85629   responsible for    5956    by helping them    

(FYI: "DO" is a column name)

I want to delete the row where its index is 571 so I did:

df=df.drop(df.index[571]) 

then I check df.ix[571]

then what the hell it's still there!

So I thought "ok, maybe index and ix are different!"

In [539]: df.index[571] 17002 

My question is

1) What is index? (compared to ix)

2) How do I delete the index row 571 using ix?

like image 410
aerin Avatar asked Oct 01 '16 01:10

aerin


People also ask

How do you drop pandas rows based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do I drop a row using ILOC?

Use iloc to get the row as a Series, then get the row's index as the 'name' attribute of the Series. Then use the index to drop.

How do I drop multiple rows in pandas based on index?

Delete a Multiple Rows by Index Position in DataFrame As df. drop() function accepts only list of index label names only, so to delete the rows by position we need to create a list of index names from positions and then pass it to drop().


2 Answers

You should drop the desired value from the index directly:

df.drop(571, inplace=True) 
like image 170
John Zwinck Avatar answered Oct 01 '22 21:10

John Zwinck


df.index 

Is the index of the dataframe.

df.index[571] 

Is the 571st element of the index. Then you dropped whatever that was. You didn't want positional but that's what you did.

Use @John Zwinck's answer

like image 20
piRSquared Avatar answered Oct 01 '22 21:10

piRSquared