如何仅获取plotly express python中的前30行

如何仅获取plotly express python中的前30行,python,pandas,plotly,Python,Pandas,Plotly,我有一个excel文件,其中有一列名为“产品”和“数量”。在产品栏中,有超过100种不同的商品(衣服、鞋子、帽子、帽子等),而数量栏显示了这些商品的销售数量 **Product** **Quantity** Shirt A 2 Shirt A 5 Shirt C 1 Shirt A 9 Shoes B 3 我想对所有不同的商品进行分组,并计算它们的总数量,但仅限于25

我有一个excel文件,其中有一列名为“产品”和“数量”。在产品栏中,有超过100种不同的商品(衣服、鞋子、帽子、帽子等),而数量栏显示了这些商品的销售数量

**Product**     **Quantity**
Shirt A             2
Shirt A             5
Shirt C             1
Shirt A             9
Shoes B             3
我想对所有不同的商品进行分组,并计算它们的总数量,但仅限于25种最畅销的商品。在熊猫中是这样的:

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名的产品出现在我面前。我该怎么做呢?

一切都在数据帧准备中

  • groupby().sum()
    以获取所需总数
  • sort_values().head()
    用于要打印的项目数。在这个例子中,我选择了前十名
  • 直方图和条形图之间没有区别

import plotly.graph_objects as go
import plotly.express as px

df = pd.DataFrame({"product":np.random.choice(list("abcdefghijklmnonpqrstuvwxyz"), 200), "quantity":np.random.uniform(3,5,200)})

df = df.groupby("product", as_index=False).sum().sort_values("quantity", ascending=False).head(10)

go.Figure(go.Bar(x=df["product"], y=df["quantity"]))
px.histogram(data_frame=df, x='product', y='quantity')