practicalGuy
practicalGuy

Reputation: 1330

Filter in pandas using operators

I have Three variables as

a='col1'
b='=='
c=2

I have Pandas dataframe as

df = pd.DataFrame({'col1': [0, 1, 2], 'col2': [10, 11, 12]},dtype=object)

And I wanted to filter for col1=2, So I wrote

df.query("@a @b @c")

Which is throwing below error

File "<unknown>", line 1
    __pd_eval_local_a __pd_eval_local_b __pd_eval_local_c
                                      ^
SyntaxError: invalid syntax

Can some one help me how to achieve this using these three variables?

Thanks,

Upvotes: 0

Views: 251

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210852

In [214]: df
Out[214]:
  col1 col2 col3
0    0   10  aaa
1    1   11  bbb
2    2   12  ccc

In [215]: a='col1'; b='=='; c=2   # <--- `c` is `int`

In [216]: df.query(a + b + '@c')
Out[216]:
  col1 col2 col3
2    2   12  ccc

In [217]: a='col3'; b='=='; c='aaa'   # <--- `c` is `str`

In [218]: df.query(a + b + '@c')
Out[218]:
  col1 col2 col3
0    0   10  aaa

it will also work with datetime dtypes, as .query() would take care of dtypes:

In [226]: df['col4'] = pd.date_range('2017-01-01', freq='99D', periods=len(df))

In [227]: df
Out[227]:
  col1 col2 col3       col4
0    0   10  aaa 2017-01-01
1    1   11  bbb 2017-04-10
2    2   12  ccc 2017-07-18

In [228]: a='col4'; b='=='; c='2017-01-01'

In [229]: df.query(a + b + '@c')
Out[229]:
  col1 col2 col3       col4
0    0   10  aaa 2017-01-01

Upvotes: 1

Tbaki
Tbaki

Reputation: 1003

You can do it without the use of query if that's not a requierement, using eval :

a='col1'
b='=='
c='2'

instruction ="df[\"" + a + "\"]" + b  + c
>>>> 'df["col1"]==2'
df[eval(instruction)]
>>>> 
    col1    col2
  2  2      12

Upvotes: 0

Related Questions