python - pandas 版本中的索引错误> = 0.20
问题描述
我想使用这个答案中的代码,因为它正是我想要的。为了我的问题的完整性,我将其粘贴在这里:
out = pd.cut(s, bins=[0, 0.35, 0.7, 1], include_lowest=True)
out_norm = out.value_counts(sort=False, normalize=True).mul(100)
ax = out_norm.plot.bar(rot=0, color="b", figsize=(6,4))
ax.set_xticklabels([c[1:-1].replace(","," to") for c in out.cat.categories])
plt.ylabel("pct")
plt.show()
我收到一个指向该ax.set_xticklabels
行的错误,指出:
TypeError: 'pandas._libs.interval.Interval' object is not subscriptable.
我知道这在熊猫版之后已经改变了0.20
。如何修改该行以执行相同的操作但避免错误?
解决方案
您可以通过调用 Interval 对象使其可下标str()
:
ax.set_xticklabels([str(c)[1:-1].replace(","," to") for c in out.cat.categories])
推荐阅读
- python - 为什么我在 pandas 访问 MySQL 时得到 Timedelta 类型
- sql - Hive - 跨列/多行的最大值
- python - Numpy 结构化数组无法进行基本的 numpy 操作
- google-apps-script - 从 AdWords 脚本将数据保存到 Google SQL(通过 JDBC)非常慢
- java - ZLIB 解压不工作
- unity3d - 统一保存输入框
- c - 当用户输入字符或字符串时如何使代码显示“无效输入”(验证)
- adb - ADB 安装失败错误 [-3001]
- ckeditor - Typo3 CKEditor 调整大小的图像保持在 300px 宽度
- .htaccess - 如何阻止我网站的所有网页被重定向到主页