Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有在数据帧查询中转换类型的方法?_Python_Pandas_Dataframe_Types_Casting - Fatal编程技术网

Python 有没有在数据帧查询中转换类型的方法?

Python 有没有在数据帧查询中转换类型的方法?,python,pandas,dataframe,types,casting,Python,Pandas,Dataframe,Types,Casting,假设我有一个数据框,有3列,全部为浮点型,将其命名为DT1。 现在,如果我想通过查询DT1从DT1创建另一个数据帧,那么第二个数据帧称为DT2 DT2 = DT1.query(‘(column1/column2) == (column3/column2)’) 只有当方程的两边完全匹配时,这才有效。 如果我只想比较两边的整数结果呢 比如: 上面的例子行不通,有解决办法吗 附言: 会有用的。我很好奇它是否能通过查询工作 谢谢 假设您有以下DF: In [125]: df Out[125]:

假设我有一个数据框,有3列,全部为浮点型,将其命名为DT1。 现在,如果我想通过查询DT1从DT1创建另一个数据帧,那么第二个数据帧称为DT2

DT2 = DT1.query(‘(column1/column2) == (column3/column2)’)
只有当方程的两边完全匹配时,这才有效。 如果我只想比较两边的整数结果呢

比如:

上面的例子行不通,有解决办法吗

附言:

会有用的。我很好奇它是否能通过查询工作


谢谢

假设您有以下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
您可以使用
DataFrame.query(…,engine='python')

或者
DataFrame.eval(…,engine='python')

检查:

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

不,它说的是TypeError:不支持的//Nice整数除法解决方案的操作数类型和引擎参数用法:-)+1@Windtalker,很高兴我能帮忙:)
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
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
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
In [131]: ((df.col1 / df.col2).astype(int) == (df.col3 / df.col2).astype(int))
Out[131]:
0     True
1    False
2     True
dtype: bool