【问题标题】:Sort Sales Data by Customer Name and Year按客户名称和年份对销售数据进行排序
【发布时间】:2021-06-23 17:06:36
【问题描述】:

我有一个包含客户名称、发货日期和采购订单金额的数据集。

我想对数据框进行排序以输出格式为的表格

cols:[Customer Name,2016,2017,2018,2019,2020,2021]
rows: 1 row for each customer and the sum of PO's within a given year.

这是我尝试过的: 数据来自 Excel 表,但假设 ShipToName 是 String,Bill Amount 是 Fl​​oat,Sell 数据是 datetime.datetime.year()。

ShipToName = ['Bob', 'Joe', 'Josh', 'Bob','Joe','Josh']
BillAmount = [30.02,23.2,20,45.32,54.23,65]
SellDate = [2016,2016,2018,2020,2021,2018]

dfSales = {'Customer': ShipToName, 'Total Sales': BillAmmount, 
'Year':SellDate}
dfSales = pd.DataFrame(dfSales,columns = ['Customer', 'Year','Total 
Sales'])
dfbyyear = dfSales.groupby(['Customer','Year'], as_index = 
False).sum().sort_values('Total Sales', ascending = False)

这为每个客户/年份组合提供了一个新行。

我希望输出看起来像:

Customer Name 2016 2017 2018 2019 2020 2021
Bob 30.02 45.32
Joe 23.20 54.23
Josh 85.00

【问题讨论】:

  • 您能分享输入数据并分享您的预期输出吗?
  • 我已经编辑了评论。抱歉,第一次发帖。
  • 看起来您需要一个包含最终结果的数据透视表。

标签: python pandas dataframe


【解决方案1】:

编辑 v2

使用原始版本中的数据,我们可以创建一个临时数据框dx,按客户名称和年份对数据进行分组。然后我们可以将数据转换为您想要的格式。

dx = df.groupby(['Customer Name','Year'])['PO Amount'].agg(Total_Amt=sum).reset_index()

dp = dx.pivot(index='Customer Name',columns='Year',values='Total_Amt')

print (dp)

这个输出将是:

Year           2020  2021
Customer Name            
Boby           6754  6371
Jack           5887  6421
Jane           5161  4411
Jill           5857  5641
Kate           6205  6457
Suzy           5027  4561

原版 v1

我正在对数据做出一些假设,因为您没有向我提供任何数据。

假设:

  1. 数据框中有很多客户 - 我的示例有 6 个 客户
  2. 每个客户有多个发货日期 - 我的示例有 1 每月发货,为期 2 年
  3. 发货金额是美元金额 - 我使用的整数范围是 100 到 900
  4. 总数据框大小为 144 行,包含 3 列 - 客户名称、发货日期和采购订单金额
  5. 您正在查找按客户、按年份、当年所有采购订单总和的输出

有了这些假设,这里是数据框和输出。

import pandas as pd
import random

df = pd.DataFrame({'Customer Name':['Jack'] * 24 + ['Jill'] * 24 + ['Jane'] * 24 + 
                                   ['Kate'] * 24 + ['Suzy'] * 24 + ['Boby'] * 24,
                   'Ship Date':pd.date_range('2020-01-01',periods = 24,freq='MS').tolist()*6,
                   'PO Amount':[random.randint(100,900) for _ in range(144)]})
print (df)
df['Year'] = df['Ship Date'].dt.year

print (df.groupby(['Customer Name','Year'])['PO Amount'].agg(Total_Amt=sum).reset_index())

    Customer Name  Ship Date  PO Amount
0            Jack 2020-01-01        310
1            Jack 2020-02-01        677
2            Jack 2020-03-01        355
3            Jack 2020-04-01        571
4            Jack 2020-05-01        875
..            ...        ...        ...
139          Boby 2021-08-01        116
140          Boby 2021-09-01        822
141          Boby 2021-10-01        751
142          Boby 2021-11-01        109
143          Boby 2021-12-01        866

每个客户都有从2020-01-012021-12-01 的数据。

总结报告如下:

   Customer Name  Year  Total_Amt
0           Boby  2020       7176
1           Boby  2021       6049
2           Jack  2020       6187
3           Jack  2021       5240
4           Jane  2020       4919
5           Jane  2021       6105
6           Jill  2020       6556
7           Jill  2021       5963
8           Kate  2020       6300
9           Kate  2021       6360
10          Suzy  2020       5969
11          Suzy  2021       4866

【讨论】:

  • 我已经编辑了帖子以包含“我在寻找什么”的表格。对不起,第一次发帖。还在努力寻找自己的出路。目前,我正在收到您的报告。我想为数据集中的每个名称和多列的年份提供单行。
  • @JoshRandles,看看更新后的答案是否能帮助您获得所需格式的结果。
  • 更新后的答案正是我所要求的。此外,有没有办法将年份列格式化为货币格式。前任。不是 6754,而是 6,754.00 美元。我可以使用地图格式在数据框中执行此操作,但不允许我对数据透视表执行此操作。
  • 我可以通过移动 pd 显示选项让数据透视表显示美元:pd.options.display.float_format='${:,.2f}'.format
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2013-07-10
相关资源
最近更新 更多