这是到目前为止我的代码:
import pandas as pd
import numpy as np
import csv
import matplotlib
import matplotlib.pyplot as plt
df = pd.read_csv('albums.csv')
group = df.groupby('genre')
data = group.aggregate(np.average)
labels = group.genre
rolling_stone_mean = data.iloc[:, 5]
mtv_mean = data.iloc[:, 6]
music_maniac_mean = data.iloc[:, 7]
x = np.arange(len(labels))
width = 0.25
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/1, rolling_stone_mean, width, label = 'Rolling Stone')
rects2 = ax.bar(x + width/1, mtv_mean, width, label = 'MTV')
rects3 = ax.bar(x, music_maniac_mean, width, label = 'Music Maniac')
ax.set_ylabel('Scores')
ax.set_title('Scores by Genre and critic')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.set_xlabel('Genre')
ax.legend()
fig.tight_layout()
plt.show()
我知道下面这一点是错误的
labels. = group.genre
本质上,我试图使用已经被用来分组的列作为xticklabel,但无法理解它,因为它似乎不是数据帧的一部分
任何帮助都将不胜感激
我想你在找我
labels = list(group.groups.keys())
“标签”仍然是DataFrame的一部分,但group不是DataFrame对象,而是groupby对象
group.groups
将为您提供标签字典和这些标签的索引相关问题 更多 >
编程相关推荐