Home > front end >  How to add mean value in pandas between each row
How to add mean value in pandas between each row

Time:12-04

Assuming you have a conventional pandas dataframe

df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])

There I would like to calculate the mean between each row. The above pandas dataframe looks like following:

>>>df2
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9

Is there an elegant solution to now calculate the mean between each two lines in order to get the following output?

>>>df2_mean
   a    b    c
0  1    2    3
1  2.5  3.5  4.5
2  4    5    6
3  5.5  6.5  7.5
4  7    8    9

CodePudding user response:

Use DataFrame.rolling with mean and concat to original:

df = pd.concat([df2.rolling(2).mean().dropna(how='all'), df2]).sort_index(ignore_index=True)
print (df)
     a    b    c
0  1.0  2.0  3.0
1  2.5  3.5  4.5
2  4.0  5.0  6.0
3  5.5  6.5  7.5
4  7.0  8.0  9.0
  • Related