Windtalker
Windtalker

Reputation: 796

Any way to cast type in pandas dataframe query?

Assume i have a dataframe with 3 columns, all in float type, name it DT1. Now if i want to create another dataframe from DT1 by query DT1, say the second one is called DT2.

DT2 = DT1.query(‘(column1/column2) == (column3/column2)’)

This will work only if two sides of the equation are exactly match. What if I only want to compare the integer results of two side?

Like:

DT2 = DT1.query(‘(column1/column2).astype(int) == (column3/column2)’).astype(int)

The example above won't work, any solution?

PS:

DT2 = DT1.loc(‘(DT1[column1]/DT1[column2]).astype(int) == (DT1[column3[/DT1[column2]).astype(int)’)

will work. I am curious if it can work through query.

Thanks!

Upvotes: 4

Views: 3163

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

assuming you have the following DF:

In [125]: df
Out[125]:
   col1  col2   col3
0  2.11   1.1  2.101
1  1.00   1.0  3.000
2  4.40   2.2  4.900

you can use DataFrame.query(..., engine='python'):

In [132]: df.query("col1 // col2 == col3 // col2", engine='python')
Out[132]:
   col1  col2   col3
0  2.11   1.1  2.101
2  4.40   2.2  4.900

or DataFrame.eval(..., engine='python'):

In [126]: df[df.eval("col1 // col2 == col3 // col2", engine='python')]
Out[126]:
   col1  col2   col3
0  2.11   1.1  2.101
2  4.40   2.2  4.900

Check:

In [131]: ((df.col1 / df.col2).astype(int) == (df.col3 / df.col2).astype(int))
Out[131]:
0     True
1    False
2     True
dtype: bool

Upvotes: 4

Related Questions