我正在尝试使用以下示例df为Profile-GeographicalZone-Town的每个组合计算每天的数量总和:
df = pd.DataFrame({
'Profile': {0: 'P014', 1: 'P014', 2: 'P012', 3: 'P012', 4: 'P012', 5: 'P012', 6: 'P012', 7: 'P012', 8: 'P012', 9: 'P012'},
'GeogaphicalZone': {0: 'NORTH', 1: 'NORTH', 2: 'NORTH', 3: 'SOUTH', 4: 'SOUTH', 5: 'SOUTH', 6: 'NORTH', 7: 'NORTH', 8: 'NORTH', 9: 'NORTH'},
'Town': {0: 'LONDON', 1: 'LONDON', 2: 'MANCHESTER', 3: 'MANCHESTER', 4: 'MANCHESTER', 5: 'MANCHESTER', 6: 'LIVERPOOL', 7: 'LIVERPOOL', 8: 'LIVERPOOL', 9: 'LONDON'},
'Quantity': {0: 8.202, 1: 8.202, 2: 8.202, 3: 60.645, 4: 60.645, 5: 60.645, 6: 90.925, 7: 162.373, 8: 45.095, 9: 78.832},
'StartDate': {0: '01/02/2019', 1: '01/01/2019', 2: '01/12/2018', 3: '01/11/2018', 4: '01/10/2018', 5: '01/09/2018', 6: '01/08/2018', 7: '01/07/2018', 8: '01/06/2018', 9: '01/05/2018'},
'EndDate': {0: '01/04/2020', 1: '01/05/2020', 2: '01/06/2020', 3: '01/07/2020', 4: '01/08/2020', 5: '01/09/2020', 6: '01/10/2020', 7: '01/11/2020', 8: '01/12/2020', 9: '01/01/2021'}
}
假设数量在开始日期和结束日期之间每天相同
现在我想要的输出是每个组合的min(StartDate)和max(EndDate)之间的每个Profile-GeographicalZone-Town的数量总和。
例如,对于P014-NORTH-LONDON组合,如果仅显示2019年1月/ 2月的日子,则我希望这样的内容:
我认为我应该对pandas使用数据透视表,但是我不确定应该如何进行Start / EndDate计算。
我可以执行第一个操作,该操作将创建一个单独的df,并在Start / EndDate之间将所有天数重复一次,然后应用数据透视表,但是我认为这不是非常有效或无效的方法。我觉得大熊猫有些合适。
有可能吗?
谢谢
答案 0 :(得分:0)
每天将其分解成一个非常长的数据帧,但这是您的操作方式:
df = pd.DataFrame({
'Profile': {0: 'P014', 1: 'P014', 2: 'P012', 3: 'P012', 4: 'P012', 5: 'P012', 6: 'P012', 7: 'P012', 8: 'P012', 9: 'P012'},
'GeogaphicalZone': {0: 'NORTH', 1: 'NORTH', 2: 'NORTH', 3: 'SOUTH', 4: 'SOUTH', 5: 'SOUTH', 6: 'NORTH', 7: 'NORTH', 8: 'NORTH', 9: 'NORTH'},
'Town': {0: 'LONDON', 1: 'LONDON', 2: 'MANCHESTER', 3: 'MANCHESTER', 4: 'MANCHESTER', 5: 'MANCHESTER', 6: 'LIVERPOOL', 7: 'LIVERPOOL', 8: 'LIVERPOOL', 9: 'LONDON'},
'Quantity': {0: 8.202, 1: 8.202, 2: 8.202, 3: 60.645, 4: 60.645, 5: 60.645, 6: 90.925, 7: 162.373, 8: 45.095, 9: 78.832},
'StartDate': {0: '01/02/2019', 1: '01/01/2019', 2: '01/12/2018', 3: '01/11/2018', 4: '01/10/2018', 5: '01/09/2018', 6: '01/08/2018', 7: '01/07/2018', 8: '01/06/2018', 9: '01/05/2018'},
'EndDate': {0: '01/04/2020', 1: '01/05/2020', 2: '01/06/2020', 3: '01/07/2020', 4: '01/08/2020', 5: '01/09/2020', 6: '01/10/2020', 7: '01/11/2020', 8: '01/12/2020', 9: '01/01/2021'}
})
df['StartDate'] = pd.to_datetime(df['StartDate'])
df['EndDate'] = pd.to_datetime(df['EndDate'])
dates = df.apply(lambda row: pd.date_range(row['StartDate'], row['EndDate']).to_series(), axis=1) \
.stack() \
.droplevel(-1)
dates.name = 'Date'
df = df.join(dates)