[英]Pandas: Calculate the average every 2 rows of a column and put it into the a new column
I want to make an average of a column, but I want the averages to be put into a new column with pandas. 我想平均列一列,但我希望将平均值放入带有pandas的新列中。
I want to go from this format: 我想从这种格式出发:
values
10
5
8
7
2
5
6
7
To this format: 对于这种格式:
values average
10 nan
5 7.5
8 6.5
7 7.5
2 4.5
5 3.5
6 5.5
7 6.5
There is a solution for something similar here: Averaging every two consecutive index values(every 2min) in pandas dataframe , but I want to keep the same number of rows. 这里有类似的解决方案: 在pandas数据帧中平均每两个连续索引值(每2分钟) ,但我想保持相同的行数。
You can use pd.Series.rolling for that ( https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html ): 您可以使用pd.Series.rolling( https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html ):
data = pd.Series([10, 5, 8, 7, 2, 5, 6, 7])
print(data.rolling(2).mean())
Output: 输出:
0 NaN
1 7.5
2 6.5
3 7.5
4 4.5
5 3.5
6 5.5
7 6.5
dtype: float64
In the other solution, the values are replacing the column in question. 在另一个解决方案中,值正在替换相关列。 I want to put them into a new column.
我想把它们放到一个新专栏中。 In the meantime, I manage to come up with a solution thanks to Paul H:
与此同时,由于Paul H,我设法找到了解决方案:
df = pd.DataFrame({'values': [10, 5, 8, 7, 2, 5, 6, 7]})
df["average"] = df["values"].rolling(2).mean()
print(df)
Output: 输出:
values average
0 10 NaN
1 5 7.5
2 8 6.5
3 7 7.5
4 2 4.5
5 5 3.5
6 6 5.5
7 7 6.5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.