My original dataframe looks like this:
No index | Value1 | Value2 | Value3 |
---|---|---|---|
0 | 1.0 | 0.0 | 0.0 |
1 | 1.0 | 0.2 | 0.2 |
2 | 1.0 | 0.4 | 0.4 |
3 | 0.8 | 0.6 | 0.6 |
4 | 0.5 | 0.4 | 0.8 |
5 | 0.1 | 0.2 | 1.0 |
And what I want to achieve is the following:
No index | Value1 | Value2 | Value3 |
---|---|---|---|
0 | 1.0 | 0.1 | 0.1 |
1 | 1.0 | 0.3 | 0.3 |
2 | 0.9 | 0.5 | 0.5 |
3 | 0.65 | 0.5 | 0.7 |
4 | 0.3 | 0.3 | 0.9 |
5 | 0.1 | 0.2 | 1.0 |
I would basically like to shift the new dataframe by 1 index, and then compute the average of the two original values. But keeping the values in the last row the same.
Is there someone who can help me with this? Thank you in advance.
CodePudding user response:
Use rolling_mean
and get values from the last row:
out = df.rolling(2).mean().shift(-1)
out.loc[len(df)-1] = df.tail(1).values
Output:
>>> out
Value1 Value2 Value3
0 1.00 0.1 0.1
1 1.00 0.3 0.3
2 0.90 0.5 0.5
3 0.65 0.5 0.7
4 0.30 0.3 0.9
5 0.10 0.2 1.0