Python Pandas DataFrame:不可排序的类型:str() > int()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24382043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:11:39  来源:igfitidea点击:

Python Pandas DataFrame: unorderable types: str() > int()

pythonstringpandasint

提问by user3770271

I have downloaded the file mentioned in book "Python for Data Analysis" and was going through the example FEC database mentioned in page 278. I get the following Type error when I ran the command. My versions: Python 3.4; Pandas: 0.14.0. OS: Windows 8

我已经下载了“Python for Data Analysis”一书中提到的文件,并且正在浏览第 278 页中提到的示例 FEC 数据库。运行命令时出现以下类型错误。我的版本:Python 3.4;Pandas:0.14.0。操作系统:Windows 8

>>> fec=pd.read_csv('c:\python\P00000001-ALL.csv')
>>> (fec.contb_receipt_amt > 0).value_counts()
>>> TypeError: unorderable types: str() > int()

But it is not just this dataset. Any dataset that I am working with have similar problem. Int(Number) data types are being imported as objects like anything else and when run any comparison with numbers(>0) on them I get the above error. What is the work around? I tried importing with dtype option, which throws an error that says int64 or Float64 is not available. I am sure there is a right way of doing. How to load the data frame with the right data types.

但这不仅仅是这个数据集。我正在使用的任何数据集都有类似的问题。Int(Number) 数据类型像其他任何东西一样作为对象导入,当对它们运行与 numbers(>0) 的任何比较时,我得到上述错误。有什么工作?我尝试使用 dtype 选项导入,这会引发一个错误,指出 int64 或 Float64 不可用。我相信有正确的做法。如何使用正确的数据类型加载数据框。

Any help is appreciated.

任何帮助表示赞赏。

回答by Denis Cottin

I got this error for some not identical dates. I solved it using a type change first.

对于某些不相同的日期,我收到了此错误。我首先使用类型更改解决了它。

Try :

尝试 :

fec[[contb_receipt_amt]] = fec[[contb_receipt_amt]].astype(str)

Then try again the count.

然后再试一次计数。