简体   繁体   中英

dataframe previous rows mean

I have the following dataframe, and want to create a new column 'measurement_mean', I expect the value of each row in the new column, to be the mean of all the previous 'Measurement' values.

How can I do this?

      Measurement  
   0          2.0  
   1          4.0  
   2          3.0  
   3          0.0  
   4        100.0  
   5          3.0  
   6          2.0  
   7          1.0  

使用pandas.Series.expanding

df[‘measurement_mean’] = df.Measurement.expanding().mean()
df['measurement_mean'] = df.Measurement.cumsum()/(df.index+1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM