Pandas; complex pivot table

I have a pandas framework that I need to change / collapse. How to do it now seems supernatural to me. The data block is as follows:

Ref Statistic Val1 Val2 Val3 Val4
 0   Mean       0    1    2    3
 0   Std        0.1  0.1  0.1  0.1
 1   Mean       0    1    2    3
 1   Std        0.1  0.1  0.1  0.1
 2   Mean       0    1    2    3
 2   Std        0.1  0.1  0.1  0.1

And I'm going to do this:

Ref Values Mean Std
 0    Val1   0  0.1
 0    Val2   1  0.1
 0    Val3   2  0.1
 0    Val4   3  0.1
 1    Val1   0  0.1
 1    Val2   1  0.1
 1    Val3   2  0.1
 1    Val4   3  0.1
 2    Val1   0  0.1
 2    Val2   1  0.1
 2    Val3   2  0.1
 2    Val4   3  0.1

It seems like it takes more than one turn or a combination of turn and grouping, but I’m out of luck ...

Any ideas?

+4
source share
2 answers
>>> df1 = pd.melt(df, value_vars=['Val1', 'Val2', 'Val3', 'Val4'],
...               id_vars=['Statistic', 'Ref'], var_name='Values')
>>> df1.pivot_table(values='value', rows=['Ref', 'Values'], cols='Statistic')
Statistic   Mean  Std
Ref Values           
0   Val1       0  0.1
    Val2       1  0.1
    Val3       2  0.1
    Val4       3  0.1
1   Val1       0  0.1
    Val2       1  0.1
    Val3       2  0.1
    Val4       3  0.1
2   Val1       0  0.1
    Val2       1  0.1
    Val3       2  0.1
    Val4       3  0.1

[12 rows x 2 columns]

if you do not want to have MultiIndex, as indicated above, you can use the method .reset_indexin the last data frame;

+5
source

Alternatively, meltyou can install MultiIndex and link the commands stackand unstack:

import pandas
# from io import StringIO # python 3
from StringIO import StringIO # python 2

datastring = StringIO('''\
Ref  Statistic  Val1  Val2  Val3  Val4
 0   Mean       0    1    2    3
 0   Std        0.1  0.1  0.1  0.1
 1   Mean       0    1    2    3
 1   Std        0.1  0.1  0.1  0.1
 2   Mean       0    1    2    3
 2   Std        0.1  0.1  0.1  0.1
''')

df = pandas.read_table(datastring, sep='\s\s+', index_col=['Ref', 'Statistic'])
df.columns.names = ['Values']
df.stack(level='values').unstack(level='Statistic')

Statistic  Mean  Std
Ref Values                
0   Val1      0  0.1
    Val2      1  0.1
    Val3      2  0.1
    Val4      3  0.1
1   Val1      0  0.1
    Val2      1  0.1
    Val3      2  0.1
    Val4      3  0.1
2   Val1      0  0.1
    Val2      1  0.1
    Val3      2  0.1
    Val4      3  0.1
+2

Source: https://habr.com/ru/post/1531145/


All Articles