首页 > 解决方案 > 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。如何修改该行以执行相同的操作但避免错误?

标签: pythonpython-3.xpandasmatplotlibplot

解决方案


您可以通过调用 Interval 对象使其可下标str()

ax.set_xticklabels([str(c)[1:-1].replace(","," to") for c in out.cat.categories])

推荐阅读