gpt4 book ai didi

python - pandas 版本 >= 0.20 绘图中的索引错误

转载 作者:行者123 更新时间:2023-12-04 09:47:45 31 4
gpt4 key购买 nike

我想使用来自 this answer 的代码,因为它正是我想要的。为了我的问题的完整性,我将其粘贴在这里:

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.

我知道在 Pandas 版本 0.20 之后这已经改变了.如何修改该行以执行相同的操作但避免错误?

最佳答案

您可以通过调用 str() 使 Interval 对象可下标。在上面:

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

关于python - pandas 版本 >= 0.20 绘图中的索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62069803/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com