Home > Net >  The Drop() method appears to do nothing to my dataframe, but no error is given
The Drop() method appears to do nothing to my dataframe, but no error is given

Time:09-17

I am using drop() to try to remove a row from a dataframe, based on an index. Nothing seems to happen. I get lots of errors if I play with the syntax, but the below example yields the same dataframe I started with.

data= {"col1":[1, 3, 3,3],"col2":[4,5,6,4],"col3":[7,6,6,8]}
testdf2 = pd.DataFrame(data)

testdf2.drop([1])
testdf2

I assume I'm missing something obvious?

CodePudding user response:

When using drop, you must reassign to your df (Or create a new one).

import pandas as pd
data= {"col1":[1, 3, 3,3],"col2":[4,5,6,4],"col3":[7,6,6,8]}
testdf2 = pd.DataFrame(data)

testdf2 = testdf2.drop([1])
testdf2

Alternatively, supply inplace.

testdf2.drop([1], inplace=True)

However, this could lead to complications regarding view / copy and I usually reassign.

Hope that helps!

  • Related