how to get the average of dataframe column values

后端 未结 1 1748
悲&欢浪女
悲&欢浪女 2020-12-14 01:10
                    A        B
DATE                 
2013-05-01        473077    71333
2013-05-02         35131    62441
2013-05-03           727    27381
2013-05-04         


        
相关标签:
1条回答
  • 2020-12-14 01:41

    Simply using df.mean() will Do The Right Thing(tm) with respect to NaNs:

    >>> df
                     A      B
    DATE                     
    2013-05-01  473077  71333
    2013-05-02   35131  62441
    2013-05-03     727  27381
    2013-05-04     481   1206
    2013-05-05     226   1733
    2013-05-06     NaN   4064
    2013-05-07     NaN  41151
    2013-05-08     NaN   8144
    2013-05-09     NaN     23
    2013-05-10     NaN     10
    >>> df.mean(axis=1)
    DATE
    2013-05-01    272205.0
    2013-05-02     48786.0
    2013-05-03     14054.0
    2013-05-04       843.5
    2013-05-05       979.5
    2013-05-06      4064.0
    2013-05-07     41151.0
    2013-05-08      8144.0
    2013-05-09        23.0
    2013-05-10        10.0
    dtype: float64
    

    You can use df[["A", "B"]].mean(axis=1) if there are other columns to ignore.

    0 讨论(0)
提交回复
热议问题