+5 votes
in Programming Languages by (77.2k points)
I want to select some rows from a pandas dataframe that satisfy multiple conditions based on columns. How can I filter rows from a dataframe?

1 Answer

+1 vote
by (354k points)
selected by
 
Best answer

There are several ways [e.g., query(), eval(), etc.] to select some rows from a pandas dataframe based on a single or multiple conditions.

I have a dataframe with columns: Name, Age, and Salary. I will select persons with a Salary> 1000 and Age> 25.

Using query()

>>> import pandas as pd
>>> df = pd.DataFrame({'Name':['AA','BB','CC','DD','EE','FF','GG','HH'], 'Age':[30,40,35,32,25,26,40,50], 'Salary':[1100,900,1000,4400,2550,6600,2177,1888]})
>>> df
  Name  Age  Salary
0   AA   30    1100
1   BB   40     900
2   CC   35    1000
3   DD   32    4400
4   EE   25    2550
5   FF   26    6600
6   GG   40    2177
7   HH   50    1888

>>> df.query("Salary>1000  & Age >25")
  Name  Age  Salary
0   AA   30    1100
3   DD   32    4400
5   FF   26    6600
6   GG   40    2177
7   HH   50    1888

Using eval()

>>> df[df.eval("Salary>1000  & Age >25")]
  Name  Age  Salary
0   AA   30    1100
3   DD   32    4400
5   FF   26    6600
6   GG   40    2177
7   HH   50    1888

Using condition on the columns:

>>> df[(df['Salary']>1000) & (df['Age']>25)]
  Name  Age  Salary
0   AA   30    1100
3   DD   32    4400
5   FF   26    6600
6   GG   40    2177
7   HH   50    1888


...