How do I create a sum row and sum column in pandas?

后端 未结 4 432
刺人心
刺人心 2020-12-08 06:55

I\'m going through the Khan Academy course on Statistics as a bit of a refresher from my college days, and as a way to get me up to speed on pandas & other scientific Py

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 07:26

    The original data is:

    >>> df = pd.DataFrame(dict(Undergraduate=[240, 3760], Graduate=[60, 440]), index=["Straight A's", "Not"])
    >>> df
    Out: 
                  Graduate  Undergraduate
    Straight A's        60            240
    Not                440           3760
    

    You can only use df.T to achieve recreating this table:

    >>> df_new = df.T
    >>> df_new
    Out: 
                   Straight A's   Not
    Graduate                 60   440
    Undergraduate           240  3760
    

    After computing the Total by row and columns:

    >>> df_new.loc['Total',:]= df_new.sum(axis=0)
    >>> df_new.loc[:,'Total'] = df_new.sum(axis=1)
    >>> df_new
    Out: 
                   Straight A's     Not   Total
    Graduate               60.0   440.0   500.0
    Undergraduate         240.0  3760.0  4000.0
    Total                 300.0  4200.0  4500.0
    

提交回复
热议问题