代码之家  ›  专栏  ›  技术社区  ›  Qubix

pandas版本图中的索引误差>=0.20

  •  0
  • Qubix  · 技术社区  · 4 年前

    我想使用来自的代码 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.
    

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

    0 回复  |  直到 4 年前
        1
  •  2
  •   mechanical_meat nazca    4 年前

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

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