0

I haven't been able to find an answer to this anywhere, so here it goes: I have a pandas dataframe df like this:

   X  Y name
0  3  1  foo
1  5  2   fa
2  1  3  hoo
3  2  4   ha

I can easily find information in df by making conditions (for example df['X' >=3]), which is great. However, I want to make a more generic solution, where I can create a long condition in string form (e.g. '(X >= 3) & (name == foo)'), which could be split into an actual condition, that can be used in a pandas dataframe. Can anyone suggest a smart solution (if something like this is possible) or redirect me to a similar discussion on the discussion board, where this topic has been debated?

kch197
  • 39
  • 7
  • The [query syntax](https://pandas.pydata.org/pandas-docs/stable/indexing.html#the-query-method-experimental) sounds like what you're interested in. – miradulo Nov 15 '17 at 13:29
  • 3
    Possible duplicate of [Efficient way to apply multiple filters to pandas DataFrame or Series](https://stackoverflow.com/questions/13611065/efficient-way-to-apply-multiple-filters-to-pandas-dataframe-or-series) – miradulo Nov 15 '17 at 13:32

1 Answers1

0

It seems you need query for filtering:

df = df.query("X >= 3 & name == 'foo'")
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252