How to delete the current row in pandas dataframe during df.iterrows()

I don't know if this is pseudo code or not but you can't delete a row like this, you can drop it:

In [425]:

df = pd.DataFrame({'a':np.random.randn(5), 'b':np.random.randn(5)})
df
Out[425]:
          a         b
0 -1.348112  0.583603
1  0.174836  1.211774
2 -2.054173  0.148201
3 -0.589193 -0.369813
4 -1.156423 -0.967516
In [426]:

for index, row in df.iterrows():
    if row['a'] > 0:
        df.drop(index, inplace=True)
In [427]:

df
Out[427]:
          a         b
0 -1.348112  0.583603
2 -2.054173  0.148201
3 -0.589193 -0.369813
4 -1.156423 -0.967516

if you just want to filter those rows out you can perform boolean indexing:

df[df['a'] <=0]

would achieve the same thing

Tags:

Python

Pandas