pandas “str”和“int”的实例之间不支持“>”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46227170/
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-14 04:27:19  来源:igfitidea点击:

'>' not supported between instances of 'str' and 'int'

pythonpython-3.xpandasdataframe

提问by

I encountered the error

我遇到了错误

'>' not supported between instances of 'str' and 'int'

“str”和“int”的实例之间不支持“>”

while trying to print the below lines in Pandas dataframe

尝试在 Pandas 数据框中打印以下行时

print (survey_df_clean.shape)
print (survey_df_clean[survey_df_clean['text']>30].shape)

Should I try to convert them to int and how would that work in this statement?

我应该尝试将它们转换为 int 以及在此语句中如何工作?

采纳答案by zimmerrol

This message suggests, that you try to compare a string object (str) with an integer (int). The expression

此消息建议您尝试将字符串对象 ( str) 与整数 ( int) 进行比较。表达方式

survey_df_clean['text']

will probably return a string. Therefore, you cannot directly compare it with the number 30. If you want to compare the length of the entry, you can use the pandas.Series.str.len()operation as you can see here.

可能会返回一个字符串。因此,您不能直接将其与 number 进行比较30。如果要比较条目的长度,可以使用此处pandas.Series.str.len()所见的操作。

If this field should actuallty contain an integer, you can use thismethod (pandas.to_numeric) to cast it from strto int.

如果该字段实际上应该包含一个整数,则可以使用方法 ( pandas.to_numeric) 将其从 转换strint

回答by Athar Noraiz

survey_df_clean['text']might have NAN or str values in it some where. to find out :

survey_df_clean['text']某些地方可能有 NAN 或 str 值。找出:

survey_df_clean['text'].isnull().sum()

if they are,first take care of them then apply

如果是,请先照顾好它们,然后再申请

print (survey_df_clean[survey_df_clean['text']>30].shape)

回答by Fariliana Eri

First make sure that all value of survey_df_clean['text'] is the same, if you want to convert as numeric, do this :

首先确保survey_df_clean['text'] 的所有值都相同,如果要转换为数字,请执行以下操作:

survey_df_clean['text'] = pd.to_numeric(survey_df_clean['text'])

Then do this

然后做这个

survey_df_clean.loc[survey_df_clean['text']>30].shape

回答by Arjun Singh

This is because values in 'text' column are of type str and you are comparing str with int. You can do a quick check for getting type of 'text' column.

这是因为 'text' 列中的值属于 str 类型,并且您正在将 str 与 int 进行比较。您可以快速检查获取“文本”列的类型。

print(type(survey_df_clean['text'][:1][0]))

For comparing you can do as following

为了进行比较,您可以执行以下操作

survey_df_clean[survey_df_clean['text'].astype(int)>30]

回答by Paulo U

I had the same error message when trying to use that conditional. What intrigued me was that the same command had run correctly on another notebook.

尝试使用该条件时,我收到了相同的错误消息。令我感兴趣的是相同的命令在另一个笔记本上正确运行。

The difference was in how I read the csv file. This was the troublesome one:

不同之处在于我读取 csv 文件的方式。这是麻烦的一个:

df=pd.read_csv('data.csv')

And when I put the decimal argument it worked:

当我输入十进制参数时,它起作用了:

df=pd.read_csv('data.csv', decimal=',')

Obviously, it'll depend on how your data is organized. ;)

显然,这取决于您的数据的组织方式。;)