I am new to pandas. I have some experimental data from multiple trials. It is all stacked vertically in a dataframe:
time var1 var2 trial
0 1 2 3 1
1 2 5 6 1
2 1 4 3 2
3 2 5 6 2
Here is the code used to produce the above dataframe:
df1 = pd.DataFrame(np.array([[1, 2, 3, 1], [2, 5, 6, 1],[1, 4, 3, 2], [2, 5, 6, 2] ]),columns=['time', 'var1', 'var2', 'trial'])
I would like to do horizontal stacking using time as the index grouping the horizontal groups by trial so that I can obtain row-wise averages of each variable at each time point across each trial:
trial 1 trial 2
time var1 var2 var1 var2
0 1 2 3 4 3
1 2 5 6 5 6
I feel like there should be an easy way to accomplish this. I have looked into pivot and stacking, but they dont accomplish what I want.