将新列添加到重新整形的数据框中

时间:2018-04-27 07:46:27

标签: python pandas

现在我有一个来自以下内容的未堆叠数据框:

                    Total_People  Age_Period   Total_income
Sge_group  History
 2015-01      1          12         2015-01      98730.77
 2015-02      1          24         2015-02      41476.93
              2          33         2015-02      84435.25
 2015-03      1          101        2015-03      40630.45
              2          99         2015-03      28969.07
              3          22         2015-03      83927.98

当我以

取消它时
SGE = SGE['Total_income'].unstack(0)

它产生了这个表,这是预期的:

sge_group   2015-01    2015-02    2013-03    
History
------------------------------------------
   1        98730.77   41476.93   40630.45
   2             nan   84435.25   28969.07
   3             nan        nan   32678.95

但我想知道我是否有可能在底部添加一两列,我可以计算每个sge_group日期的总人数或一个总收入,看起来像这样: / p>

sge_group   2015-01    2015-02    2013-03    
History
------------------------------------------
   1        98730.77   41476.93   40630.45
   2             nan   84435.25   28969.07
   3             nan        nan   32678.95
Total_People      12         57        222
Total_Income       x          x          x

1 个答案:

答案 0 :(得分:2)

MultiIndex的第一级使用groupbyaggsum汇总,转置,上次append到原始DataFrame:< / p>

d = SGE['Total_income'].unstack(0)
print (d)
Sge_group   2015-01   2015-02   2015-03
History                                
1          98730.77  41476.93  40630.45
2               NaN  84435.25  28969.07
3               NaN       NaN  83927.98

df = SGE.groupby(level=0)['Total_People','Total_income'].sum().T
print (df)
Sge_group      2015-01    2015-02   2015-03
Total_People     12.00      57.00     222.0
Total_income  98730.77  125912.18  153527.5

SGE = d.append(df)
#alternative
#SGE = pd.concat([d, df])

print (SGE)
Sge_group      2015-01    2015-02    2015-03
1             98730.77   41476.93   40630.45
2                  NaN   84435.25   28969.07
3                  NaN        NaN   83927.98
Total_People     12.00      57.00     222.00
Total_income  98730.77  125912.18  153527.50