我有两个这样的数据框:
Dataframe 1:
Date A B C D
2019-06-01 2 0 3 1
2019-06-02 3 1 2 0
2019-06-09 1 2 4 1
2019-06-10 1 2 0 1
Dataframe 2:
Date A B C D
2019-06-01 1 0 2 1
2019-06-02 1 3 1 0
2019-06-09 1 0 2 1
2019-06-10 0 2 0 1
我必须连接两个数据框并按周排列,然后将每天的列条目添加到相应的周中,如下所示:
Output df:
Date A B C D
01-08 JUN19 7 4 8 2
08-15 JUN19 3 6 6 4
我的代码是这样写的:
df1 = pd.read_excel(xls, 'table1')
df2 = pd.read_excel(xls, 'table2')
df_consolidated = pd.concat([df1, df2], ignore_index=True)
df_consolidated['Date'] = pd.to_datetime(df_consolidated['Date'])
df_consolidated = df_consolidated.groupby([pd.Grouper(key='Date', freq='W-SAT', closed='left'),
'A','B','C','D'])['Date'].sum()
chng = ((df_consolidated.index.levels[0] - pd.to_timedelta(7, unit='d')).strftime('%d') + ' - '+
df_consolidated.index.levels[0].strftime('%d %b%y').str.upper())
df_consolidated = df_consolidated.index.set_levels(chng, level=0)
我得到的输出类似于多重索引,甚至无法用excel编写:
MultiIndex(levels=[['01 - 08 JUN19', '08 - 15 JUN19', '15 - 22 JUN19', '22 - 29 JUN19', '29 - 06 JUL19', '06 - 13 JUL19', '13 - 20 JUL19', '20 - 27 JUL19', '27 - 03 AUG19', '03 - 10 AUG19', '10 - 17 AUG19', '17 - 24 AUG19', '24 - 31 AUG19', '31 - 07 SEP19', '07 - 14 SEP19', '14 - 21 SEP19', '21 - 28 SEP19', '28 - 05 OCT19', '05 - 12 OCT19', '12 - 19 OCT19', '19 - 26 OCT19', '26 - 02 NOV19', '02 - 09 NOV19', '09 - 16 NOV19', '16 - 23 NOV19', '23 - 30 NOV19'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
请突出显示我要去哪里以及如何获取输出数据帧,如上所示。需要对此的见识