gpt4 book ai didi

python - 我需要从 excel 文件中找出订单数量最多的 5 个日期

转载 作者:行者123 更新时间:2023-12-04 20:50:18 24 4
gpt4 key购买 nike

here is the example of the question

 import pandas as pd
# read excel file
df = pd.read_excel("C:/Users/samer/Desktop/project/online_retail2.xlsx")
df.head()
# is gonna remove nan from the equation
df = df.dropna()

columns = list(df.columns)
columns = [x.lower() for x in columns]
df.columns = columns
#task 2.1
df['stockcode'] = df['stockcode'].apply(str)

df["products ordered by individuals"] = df["description"] + [' '] + df["stockcode"]

top_products = df['products ordered by individuals'].value_counts()[:10]
print('Top 10 products ordered by individuals:\n' , top_products)

#task 2.2
df['sales'] = df['price'] * df['quantity']
The_most_money_spent= df.groupby("customer id").sum().sort_values("sales", ascending=False).head(10)
print("Top ten customers who spent most money are: \n" ,The_most_money_spent)


#task 2.3
top_orders_were_placed = df[['invoicedate','sales']].value_counts('sales','invoicedate',
ascending=False)
top_orders_were_placed.head(5)


df['orders']= df[['sales', 'invoicedate']]
top_orders_were_placed= df['orders'].value_counts(ascending=False)
top_orders_were_placed.head()
我试图找到一种方法来获得最多的产品,但我只是不知道如何使用或从文件中找到日期。尽管进行了多次尝试,但我还是想不通,已经 2 天了,那么我到底应该使用什么?

最佳答案

您需要使用 pandas 的 groupby 功能。您需要按日期分组,然后汇总数量。之后,您只需要按降序对值进行排序。

关于python - 我需要从 excel 文件中找出订单数量最多的 5 个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65543697/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com