【问题标题】:TypeError: unsupported operand type(s) for -: 'str' and 'float' python pandasTypeError: 不支持的操作数类型 -: 'str' 和 'float' python pandas
【发布时间】:2019-01-27 01:23:11
【问题描述】:

我有一个数据框,我使用 groupby 来获取下表

    time_groups=time_data.groupby('time_binned').mean()
    time_groups

然后我尝试构建一个条形图来显示“状态”列结果, 但我收到错误 TypeError: unsupported operand type(s) for -: 'str' and 'float'

plt.bar(time_groups.index.astype(str),100*time_groups['Status'])

【问题讨论】:

  • 我认为你不应该将字符串作为第一个参数传递给plt.bar。而是使用range(len(time_groups.index)) 之类的东西,稍后通过plt.xticks 调整标签。
  • time_groups[['status']].plot.bar()?

标签: python pandas


【解决方案1】:

您看到错误的原因是第一个参数 (x) 必须是数字。这是因为您要指定绘图的 x 坐标。尝试这样做:

plt.bar(x = np.arange(0, len(time_groups['Status'])), height = time_groups['Status'])
plt.xticks(np.arange(0, len(time_groups['Status'])), time_groups.index.values)
plt.show()

【讨论】:

  • 嗨毗湿奴,它显示另一个错误栏()缺少 1 个必需的位置参数:'left'
  • 快速提问 Kunchur,如果我想使用 time_binned 列数据作为我的 xsticks,你知道这样做的好方法是什么吗?
  • 它解决了,我使用了你上面写的代码,它可以工作。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2013-10-29
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多