【问题标题】:TypeError: unsupported operand type(s) for /: 'str' and 'int' (2)TypeError: 不支持的操作数类型 /: 'str' 和 'int' (2)
【发布时间】:2020-01-14 08:05:54
【问题描述】:

我正在尝试显示我的数据集中包含数字和字母的图表。

grouped = pd.DataFrame(train.groupby(['shop_id', 'date_block_num'])['item_cnt_day'].sum().reset_index())
fig, axes = plt.subplots(nrows=5, ncols=2, sharex=True, sharey=True, figsize=(16,20))
num_graph = 10
id_per_graph = ceil(grouped.shop_id.max() / num_graph)
count = 0
for i in range(5):
    for j in range(2):
        sns.pointplot(x='date_block_num', y='item_cnt_day', hue='shop_id', data=grouped[np.logical_and(count*id_per_graph <= grouped['shop_id'], grouped['shop_id'] < (count+1)*id_per_graph)], ax=axes[i][j])
        count += 1

然后,它说:

TypeError                                 Traceback (most recent call last)
<ipython-input-19-77428a365041> in <module>
      2 fig, axes = plt.subplots(nrows=5, ncols=2, sharex=True, sharey=True, figsize=(16,20))
      3 num_graph = 10
----> 4 id_per_graph = ceil(grouped.shop_id.max() / num_graph)
      5 count = 0
      6 for i in range(5):

TypeError: unsupported operand type(s) for /: 'str' and 'int'

【问题讨论】:

  • 数据框'shop_id'列的值是数字吗?
  • 不,是数字和字母的组合
  • 这就是错误的原因 - 您不能在混合了字符串和数字的列表上调用 max()。你想用ceil(grouped.shop_id.max() / num_graph) 完成什么?

标签: python pandas dataframe matplotlib


【解决方案1】:

试试id_per_graph = ceil(max(grouped.shop_id) / num_graph) 你用错了.max()

【讨论】:

  • 在开头添加import math并像这样调用ceil math.ceil()
  • TypeError: /: 'str' 和 'int' 的操作数类型不受支持
猜你喜欢
  • 2016-04-15
  • 2012-11-29
  • 2012-12-31
  • 1970-01-01
  • 2021-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多