【发布时间】: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 是 Float,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 |
【问题讨论】:
-
您能分享输入数据并分享您的预期输出吗?
-
我已经编辑了评论。抱歉,第一次发帖。
-
看起来您需要一个包含最终结果的数据透视表。