简体   繁体   English

数据框前几行均值

[英]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. 我有以下数据框,并希望创建一个新列“ measurement_mean”,我希望新列中每一行的值都是所有先前“ Measurement”值的均值。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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