Suppose we have a very simple dataframe.
0 1 20
1 2 30
2 5 50
3 6 90
4 7 80
The question is simple: How do we create a third row 'C' such that the following is true?
df.C[0] = mean of all the 10 numbers
df.C[1] = mean of 2, 5, 6, 7, 30, 50, 90, 80
df.C[2] = mean of 5, 6, 7, 50, 90, 80
df.C[3] = mean of 6, 7, 90, 80
df.C[4] = mean of 7, 80
I've read dozens of relevant tutorials online but all of them only teach how to get a single mean for a single row.
Any help would be much appreciated.
import pandas as pd df = pd.DataFrame({'A': [1, 2, 5, 6, 7], 'B': [20, 30, 50, 90, 80]}) print(df)A B
0 1 20
1 2 30
2 5 50
3 6 90
4 7 80
The question is simple: How do we create a third row 'C' such that the following is true?
df.C[0] = mean of all the 10 numbers
df.C[1] = mean of 2, 5, 6, 7, 30, 50, 90, 80
df.C[2] = mean of 5, 6, 7, 50, 90, 80
df.C[3] = mean of 6, 7, 90, 80
df.C[4] = mean of 7, 80
I've read dozens of relevant tutorials online but all of them only teach how to get a single mean for a single row.
Any help would be much appreciated.