Concat DataFrame 到多索引 df 保持原始多索引

Concat DataFrame to multi index df keeping original multi index

我有 2 个来自预算财务项目的数据框,其中一个是记录支出的 df;

                          Food  Clothes  Bills  Social  Travel  Art  Other  Daily Total
Week End   Today's Date
2020-09-27 2020-09-21     25       25     25      25      25   25     25          175
           2020-09-23     20       20     20      20      20   20     20          140
           2020-09-24     12       12     12      12      12   12     12           84
           2020-09-25     20       20     20      20      20   20     20          140

每周总计之一;

     Food  Clothes  Bills  Social  Travel  Art  Other  Daily Total
0    77       77     77      77      77   77     77          539

我想连接它们,同时保持原来的多索引看起来像这样;

                          Food  Clothes  Bills  Social  Travel  Art  Other  Daily Total
Week End   Today's Date
2020-09-27 2020-09-21     25       25     25      25      25   25     25          175
           2020-09-23     20       20     20      20      20   20     20          140
           2020-09-24     12       12     12      12      12   12     12           84
           2020-09-25     20       20     20      20      20   20     20          140
                0         77       77     77      77      77   77     77          539

如果我执行基本的 concat 函数,多索引会变成一堆元组,例如;

                                              Food  Clothes  Bills  Social

(2020-09-27 00:00:00, 2020-09-21 00:00:00)     25       25     25      25 
etc.

 

有什么想法吗??对 pandas 还很陌生,而且编码一般,因此非常感谢任何帮助。

我更改了列标题以使其更易于使用。 周末 = Week_End 今天的日期 = Todays_Date Daily_Total

您可以只附加行

df2 包含您的每周总计

#set an index to match your recorded spending df
df2['Week_End'] = '2020-09-27'
df2['Todays_Date'] = '0'
df2.set_index(['Week_End', 'Todays_Date'], inplace=True)


#append df2 to df
df = df.append(df2)

结果:

                        Food  Clothes  Bills  Social  Travel  Art  Other  Daily_Total
Week_End   Todays_Date                                                               
2020-09-27 2020-09-21     25       25     25      25      25   25     25          175
           2020-09-23     20       20     20      20      20   20     20          140
           2020-09-24     12       12     12      12      12   12     12           84
           2020-09-25     20       20     20      20      20   20     20          140
           0              77       77     77      77      77   77     77          539