python - 在 str 和 int 的实例之间不支持
问题描述
我有这个代码
data['A'].loc[data['A']>30] = 'high'
为我工作。但是,当我使用
data['A'].loc[data['A']<30] = 'low'
弹出错误信息
'<' not supported between instances of 'str' and 'int'
我希望在同一熊猫列上具有'high'
高于 30的值和低于 30 的值。'low'
解决方案
代替手动方法,使用pd.cut
:
pd.cut(data['A'], [float('-inf'), 30, float('inf')], labels=['low', 'high'])
例子:
s = pd.Series([-10, 40, 70, 60, 20])
pd.cut(s, [float('-inf'), 30, float('inf')], labels=['low', 'high'])
输出:
0 low
1 high
2 high
3 high
4 low
推荐阅读
- python - 是否有允许团队通过类似 git 的界面共享 venvs 的 python 包?
- microcontroller - 为什么我们在信号量中使用 ISR 函数?
- twitter - Tweepy 机器人发推特 DM
- javascript - 如何在隐藏元素上停止 setinterval
- pandas - 按最多三列对 pandas 数据框中的示例进行排序
- hex - \x00@ 是什么意思?
- java - 从活动启动 Fragment 活动会导致 NullPointerException
- javascript - 制作用于将 URL 字符串转换为 JSON 的递归算法
- gitlab - 如何解决 GitLab 项目导出后管道损坏的问题?
- angular - 不完全禁用每个文件的严格模板