1

For example, I have lists of column names, the equal relations and conditions

column = ['height', 'age', 'gender']
equal = ['>', '>', '==']
condition = [1.68, 20, 'F']

And I want to select the data with specific conditions in pandas DataFrame,

df = df[(df['height']>1.68) & df['age']>20 & df['gender']=='F']

Is it possible to use the three lists above (columns, equality, and condition) for realizing the selction?

user19881219
  • 317
  • 2
  • 16

1 Answers1

2

I think need query for built conditions, but is necessary if-else condition for add "" for strings values in values:

df = pd.DataFrame({'gender':list('MMMFFF'),
                   'height':[4,5,4,5,5,4],
                   'age':[70,80,90,40,2,3]})

print (df)
  gender  height  age
0      M       4   70
1      M       5   80
2      M       4   90
3      F       5   40
4      F       5    2
5      F       4    3

q = ' & '.join(['{}{}"{}"'.format(i,j,k) if isinstance(k, str) 
               else '{}{}{}'.format(i,j,k) for i, j, k in zip(column, equal, condition)])
print (q)
height>1.68 & age>20 & gender=="F"

Thank you @Yakym Pirozhenko for simplier solution:

q = ' & '.join(['{}{}{}'.format(i,j,repr(k)) for i, j, k in zip(column, equal, condition)])
print (q)
height>1.68 & age>20 & gender=='F' 

df = df.query(q)
print (df)
  gender  height  age
3      F       5   40
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252