Add column to the end of Pandas DataFrame containing average of previous data

前端 未结 4 1286
深忆病人
深忆病人 2021-02-12 14:04

I have a DataFrame ave_data that contains the following:

ave_data

Time        F7           F8            F9  
00:00:00    43.005593    -56.509746           


        
4条回答
  •  心在旅途
    2021-02-12 14:50

    You can take a copy of your df using copy() and then just call mean and pass params axis=1 and numeric_only=True so that the mean is calculated row-wise and to ignore non-numeric columns, when you do the following the column is always added at the end:

    In [68]:
    
    summary_ave_data = df.copy()
    summary_ave_data['average'] = summary_ave_data.mean(numeric_only=True, axis=1)
    summary_ave_data
    Out[68]:
                     Time         F7         F8         F9    average
    0 2015-07-29 00:00:00  43.005593 -56.509746  25.271271   3.922373
    1 2015-07-29 01:00:00  55.114918 -59.173852  31.849262   9.263443
    2 2015-07-29 02:00:00  63.990762 -64.699492  52.426017  17.239096
    

提交回复
热议问题