6 python dataframe python-3.x pandas
我遇到了错误
'str' 和 'int' 实例之间不支持 '>'
尝试在 Pandas 数据框中打印以下行时
print (survey_df_clean.shape)
print (survey_df_clean[survey_df_clean['text']>30].shape)
Run Code Online (Sandbox Code Playgroud)
我应该尝试将它们转换为 int 吗?在这个语句中这将如何工作?
小智 6
首先确保 Survey_df_clean['text'] 的所有值都相同,如果要转换为数字,请执行以下操作:
survey_df_clean['text'] = pd.to_numeric(survey_df_clean['text'])
Run Code Online (Sandbox Code Playgroud)
然后这样做
survey_df_clean.loc[survey_df_clean['text']>30].shape
Run Code Online (Sandbox Code Playgroud)
此消息建议您尝试将字符串对象 ( str) 与整数 ( int) 进行比较。表达方式
survey_df_clean['text']
Run Code Online (Sandbox Code Playgroud)
可能会返回一个字符串。因此,不能直接与数字进行比较30。如果您想比较条目的长度,可以使用此处pandas.Series.str.len()所示的操作。
如果该字段实际上应该包含整数,则可以使用此方法 ( pandas.to_numeric) 将其从 转换str为int。
| 归档时间: |
|
| 查看次数: |
67195 次 |
| 最近记录: |