【发布时间】:2021-05-22 19:08:36
【问题描述】:
我有一个 Excel 文件,其中有一列名为“产品”和“数量”。在 Product 列中,有超过 100 种不同的商品(衣服、鞋子、帽子、帽子等),而 Quantity 列显示了这些产品的销售量。
**Product** **Quantity**
Shirt A 2
Shirt A 5
Shirt C 1
Shirt A 9
Shoes B 3
我想对所有不同的商品进行分组并计算它们的总数量,但仅针对最畅销的 25 种商品。在 pandas 中会是这样的:
df = pd.read_csv('directory\Sales.csv')
df_products = df[['Product',
'Quantity']].groupby('Product').sum().head(25).sort_values(by='Quantity', ascending=False)
但是我怎样才能在 plotly.express 制作的直方图中做同样的事情呢?我试过这个:
fig_product = px.histogram(data_frame=df_products, x='Product', y='Quantity')
这向我显示了所有 +100 产品的名称及其销售数量,但我只想显示其中的前 25 个。我该怎么做?
【问题讨论】: