总的来说,我是Python的新手,我正在进行一个项目,但我遇到了一些障碍,需要一双新的眼睛来检查它。
本质上,我尝试将一个名为“Description”的列组合在一起,然后将该列的所有数量相加,并按从大到小的顺序进行排序。然后,我想将这一点可视化到一个条形图中。我已经生成了条形图,问题是x轴显示了错误的产品描述。我将链接一些我想说的图片:https://imgur.com/a/zpJpChI。
下面是我正在使用的代码:
product_group = olS.groupby('Description')
product_group.sum().sort_values(by='Quantity', ascending = False)
quantity_ordered = product_group.sum()['Quantity'].sort_values(ascending = False)
products = [product for product, products in product_group]
plt.bar(products, quantity_ordered)
plt.ylabel('Quantity Ordered')
plt.xlabel('Description')
plt.xticks(products, rotation='vertical', size=10)
plt.xlim(0,10)
plt.show()感谢您的支持。
发布于 2021-07-08 18:59:14
我创建了自己的数据集,只有两列"Quantity“和"Description":
df1 = df.groupby('Description').sum(['Quantity']).sort_values(by='Quantity',ascending = False)
plt.bar(df1.index, df1['Quantity'])
plt.ylabel('Quantity Ordered')
plt.xlabel('Description')
plt.xticks(df1.index, rotation='vertical', size=10)
plt.xlim(0,10)
plt.show()另一个选项是重置索引,如下所示:
df1 = df.groupby('Description').sum(['Quantity']).sort_values(by='Quantity',ascending = False).reset_index()并用df1['Description']替换df1.index。
https://stackoverflow.com/questions/68299998
复制相似问题