Pandas DataFrame : How to select rows on multiple conditions?
we can use DataFrame.query() method like this:
In [109]: dct = {'name': 4.0, 'sex': 0.0, 'city': 2, 'age': 3.0}
In [110]: qry = ' and '.join(['{} <= {}'.format(k,v) for k,v in dct.items()])
In [111]: qry
Out[111]: 'name <= 4.0 and sex <= 0.0 and city <= 2 and age <= 3.0'
In [112]: df.query(qry)
...
You could take advantage of Pandas' automatic axis alignment. Given a DataFrame with columns ['age', 'city', 'name', 'sex']
and a Series with the same index, you can compare every entry in the DataFrame against the corresponding value in the Series using
In [29]: df < pd.Series(dct)
Out[29]:
age city name sex
0 False False False False
1 False False False False
2 True False False False
3 False True False False
4 True True True False
...
Then you can find the rows which are all True
using
mask = (df <= pd.Series(dct)).all(axis=1)
and select those rows with df.loc[mask, :]
. For example,
import numpy as np
import pandas as pd
np.random.seed(2017)
N = 300
df = pd.DataFrame({'name':np.random.randint(10, size=N),
'sex':np.random.randint(2, size=N),
'city':np.random.randint(10, size=N),
'age':np.random.randint(10, size=N)})
dct = {'name': 4.0, 'sex': 0.0, 'city': 2, 'age': 3.0}
mask = (df <= pd.Series(dct)).all(axis=1)
print(df.loc[mask, :])
yields
age city name sex
7 3 2 0 0
10 1 2 4 0
150 1 2 4 0
188 2 2 2 0
198 3 2 3 0
229 1 2 0 0
254 1 2 2 0
275 3 2 1 0
276 0 1 4 0
299 3 1 2 0