Pandas 类型错误:'&燃气轮机';在';str';和';int';熊猫

Pandas 类型错误:'&燃气轮机';在';str';和';int';熊猫,pandas,Pandas,我正在比较a、a和B中的两列,如下所示: df['A'].gt(df['B']) 但是得到这个错误: TypeError: '>' not supported between instances of 'str' and 'int' TypeError:'str'和'int'的实例之间不支持'>' 查找数据类型时: df.dtypes 我看到这个输出: id: object A: object B: int64 当我看到5项顶级记录时 dodf.head() 我看到: id A

我正在比较a、a和B中的两列,如下所示:

df['A'].gt(df['B'])
但是得到这个错误:

TypeError: '>' not supported between instances of 'str' and 'int'
TypeError:'str'和'int'的实例之间不支持'>'

查找数据类型时: df.dtypes

我看到这个输出:

id: object
A: object
B: int64
当我看到5项顶级记录时 dodf.head()

我看到:

id    A   B
a1    2   2.353566677998
a2    4   4.454444231211
a3    3   6.777888665343
.....
如何解决此错误:

TypeError: '>' not supported between instances of 'str' and 'int'
进行此比较时:

df['A'].gt(df['B'])


看起来很多人都在问这个问题,但我找不到一个与我的问题相匹配的

尽管在查看数据帧时,这些值显然是整数,但pandas对
str
值使用
object
,这是由于错误造成的。不过,您可以将
A
列的所有值强制转换为整数,您应该可以开始了


df[“A”]=df[“A”].astype(int)

在比较之前尝试
df['A']=pd.to_numeric(df['A'],errors='concurve')
?这一个成功了,谢谢Quang Hoang这一个返回了一个错误“int()的文本无效,基数为10…”,但无论如何,谢谢Alex Watt这是正确的答案。请显示@alexr收到的完整错误消息。这可能是一个
ValueError
,然后它应该显示第一个中断转换的值。另外,尝试使用
float
而不是
int