13

I have the below dataframe

In [62]: df Out[62]: coverage name reports year Cochice 45 Jason 4 2012 Pima 214 Molly 24 2012 Santa Cruz 212 Tina 31 2013 Maricopa 72 Jake 2 2014 Yuma 85 Amy 3 2014 

Basically i can filter the rows as below

df[df["coverage"] > 30 

and i can drop/delete a single row as below

df.drop(['Cochice', 'Pima']) 

But i want to delete a certain number of rows based on a condition, how can i do so?

4
  • Can you explain more your condition? Commented Jan 24, 2017 at 16:42
  • I want to delete rows if the value of coverage column in less than 72 Commented Jan 24, 2017 at 16:44
  • Then use boolean indexing - df[df["coverage"] >= 72] Commented Jan 24, 2017 at 16:44
  • Yes i got that, just curious if i can get more ways and so posted here :) Commented Jan 24, 2017 at 16:45

2 Answers 2

18

The best is boolean indexing but need invert condition - get all values equal and higher as 72:

print (df[df["coverage"] >= 72]) coverage name reports year Pima 214 Molly 24 2012 Santa Cruz 212 Tina 31 2013 Maricopa 72 Jake 2 2014 Yuma 85 Amy 3 2014 

It is same as ge function:

print (df[df["coverage"].ge(72)]) coverage name reports year Pima 214 Molly 24 2012 Santa Cruz 212 Tina 31 2013 Maricopa 72 Jake 2 2014 Yuma 85 Amy 3 2014 

Another possible solution is invert mask by ~:

print (df["coverage"] < 72) Cochice True Pima False Santa Cruz False Maricopa False Yuma False Name: coverage, dtype: bool print (~(df["coverage"] < 72)) Cochice False Pima True Santa Cruz True Maricopa True Yuma True Name: coverage, dtype: bool print (df[~(df["coverage"] < 72)]) coverage name reports year Pima 214 Molly 24 2012 Santa Cruz 212 Tina 31 2013 Maricopa 72 Jake 2 2014 Yuma 85 Amy 3 2014 
Sign up to request clarification or add additional context in comments.

Comments

2

we can use pandas.query() functionality as well

import pandas as pd dict_ = {'coverage':[45,214,212,72,85], 'name': ['jason','Molly','Tina','Jake','Amy']} df = pd.DataFrame(dict_) print(df.query('coverage > 72')) 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.