【发布时间】: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