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

标签 python excel

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/

相关文章:

python - 文件存在时,os.rename 不会引发 FileExistsError

python - 如何让 Python 代码在 telnet (telnetlib) 超时后继续

Excel VBA - 将列表作为函数参数传递

python - 从 Pandas/Python 中选定的单元格访问索引/行/列

javascript - 从时间戳和电子邮件地址转换唯一 ID

Excel VBA 事件根据用户输入进行计算

python - 当您从 Python 中的模块而不是类继承时会发生什么?

python - 如何从 Django 中的 request.FILE 获取文件名?

python - 无法重现 Xgb.cv 交叉验证结果

excel - 我可以使用 Perl 从 PDF 中提取表格吗?