0

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.

Stone Preston
  • 1,828
  • 4
  • 20
  • 35
  • `df.pivot('time', 'trial', ['var1', 'var2']).swaplevel(0, 1, axis=1)`. Sort the first level of your first axis to get the exact output. – user3483203 Oct 23 '19 at 19:17
  • You can do the same thing with `df.set_index(['time', 'trial']).unstack(-1)` and then swaplevels/sort – user3483203 Oct 23 '19 at 19:22
  • alright thanks!, is there a quick way to take the mean of the values of var1, and then the mean of the values of var2 and list them instead? So instead of having 2 columns for the values of var1 and 2 columns for the values of var2, just have one column for the mean of the var 1's and one column for the mean of var2s – Stone Preston Oct 23 '19 at 19:49
  • Sure, use `df.pivot('time', 'trial', ['var1', 'var2']).groupby(level=0, axis=1).mean()` – user3483203 Oct 23 '19 at 19:50
  • perfect, if you add your comments as an answer I will accept it – Stone Preston Oct 23 '19 at 21:07

0 Answers0