0

I have DataFrame that looks like this

     exec       ms        tp    lu    ru
0   exec1     16.0    240.87  2.30  0.85
1   exec1     16.0    243.72  2.35  0.84
2   exec1     16.0    234.16  2.38  0.92
3   exec1     16.0    244.71  2.35  0.84
4   exec1     16.0    240.74  2.39  0.90
5   exec1    128.0   1686.78  2.09  0.69
6   exec1    128.0   1704.36  2.00  0.44
7   exec1    128.0   1686.45  2.07  0.60
8   exec1    128.0   1722.61  2.07  0.45
9   exec1    128.0   1726.15  2.08  0.50
10  exec1   1024.0   5754.92  2.23  0.93
11  exec1   1024.0   5740.71  2.24  0.93
12  exec1   1024.0   5751.58  2.24  0.96
13  exec1   1024.0   5819.63  2.23  0.92
14  exec1   1024.0   5797.03  2.22  0.96
15  exec1   8192.0  37833.45  1.91  3.87
16  exec1   8192.0  38154.95  2.00  3.87
17  exec1   8192.0  38178.19  2.02  3.85
18  exec1   8192.0  38152.86  1.95  3.84
19  exec1   8192.0  35209.98  1.80  3.65
20  exec1  16384.0  38109.76  1.81  3.84
21  exec1  16384.0  38059.07  1.76  3.90
22  exec1  16384.0  36683.24  1.54  3.71
23  exec1  16384.0  37908.00  1.73  3.85
24  exec1  16384.0  37014.79  1.71  3.75

and I would like to make columns from ms for data from tp, lu and ru and have them as hierarchical columns and use exec as the index like this:

                                               lu                                          ru                                         tp
exec    16.0    128.0   1024.0  8192.0  16384.0     16.0    128.0   1024.0  8192.0  16384.0     16.0    128.0   1024.0  8192.0  16384.0
exec1   2.30    2.09    2.23    1.91    1.81    0.85    0.69    0.93    3.87    3.84    240.87  1686.78     5754.92     37833.45    38109.76
exec1   2.35    2.00    2.24    2.00    1.76    0.84    0.44    0.93    3.87    3.90    243.72  1704.36     5740.71     38154.95    38059.07
exec1   2.38    2.07    2.24    2.02    1.54    0.92    0.60    0.96    3.85    3.71    234.16  1686.45     5751.58     38178.19    36683.24
exec1   2.35    2.07    2.23    1.95    1.73    0.84    0.45    0.92    3.84    3.85    244.71  1722.61     5819.63     38152.86    37908.00
exec1   2.39    2.08    2.22    1.80    1.71    0.90    0.50    0.96    3.65    3.75    240.74  1726.15     5797.03     35209.98    37014.79

I tried using pd.pivot_table but it creates unwanted nans.

1 Answers1

0

May need groupby +cumcount create a additional key , then do pivot transform , here I am using unstack , if you need check pivot , personally think this explain better than the official document

df.assign(key=df.groupby(['exec','ms']).cumcount()).set_index(['exec','ms','key']).unstack([1])
BENY
  • 317,841
  • 20
  • 164
  • 234