0

I have an interface which gets a str with the condition to be executed.

Is it possible to directly pass this str in my pandas loc?

example:

df:
{'col A': [1, 2, 3, 4],
'col B': ['a', 'b', 'c', 'd']}

entry_str = "col A == 2"

df = df.loc[entry_str]
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Eolynas
  • 19
  • 5
  • What's your desired output? – Ynjxsjmh Oct 13 '20 at 10:43
  • I want to retrieve the lines that match the filter – Eolynas Oct 13 '20 at 12:14
  • One possible way to do this is to split the `entry_str` with comparison operators like `==`, `>` or `>=`. Then use answers for [this question](https://stackoverflow.com/q/17071871/10315163). I think it will be more easier if you can seperate column name, comparison operators and value from `entry_str` in your interface. – Ynjxsjmh Oct 13 '20 at 14:02
  • If you want to turn string type operators to function, you can refer to [this question](https://stackoverflow.com/q/1740726/10315163). You could visit https://docs.python.org/3/library/operator.html for more operators. – Ynjxsjmh Oct 13 '20 at 14:35

1 Answers1

0

just use df.query('col A == 2')

Qas
  • 332
  • 4
  • 12