0

I have a dataframe named df

   a       b  
0  str1    0
1  str2   .8
2  str3   .4
3  str4   .1

I am iterating through this dataframe. (which i know is not the most efficient way).

I want to drop all rows where b>.7. This is iterating in a loop, so i want to drop from the same dataframe.

The append has syntax like this:

new_df = new_df.append(df[df['a']>.7],ignore_index= 'True')

Can I do something similar with the drop?

df.drop(df[df['a']>.7])

I get the error: ".... not found in axis"

The expected result is df as:

   a       b  
0  str1    0
2  str3   .4
3  str4   .1
niraj
  • 17,498
  • 4
  • 33
  • 48

1 Answers1

0

It's very easy. and there are multiple ways using Boolean indexing:

1) Retain rows where b is less than or equal to 7

df = df[df.b<=0.7]

2) Use .loc for the same condition

df = df.loc[df.b<=0.7]

3) Filter out rows which are greater than 0.7

df=df[~df.b>0.7]
Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45