Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiIndex Pandas From Dataframe

How would I take a df like this:

Dates          Type           1      2      3                                                                                                                            ...
2018-01-01     Type1        Golf    Van    Jeep
2018-01-02     Type1        Golf    Van    Jeep
2018-01-01     Type2        Golf1   Van1   Jeep1
2018-01-02     Type2        Golf2   Van2   Jeep2

and turn it into:

                               Type1                    Type2
Dates                    1      2      3           1      2      3                                                                                                               ...
2018-01-01            Golf    Van    Jeep      Golf1    Van1    Jeep1
2018-01-02            Golf    Van    Jeep      Golf2    Van2    Jeep2

EDIT: I would like to introduce a second index like this:

Type                          Type1                    Type2
Numbers                  1      2      3           1      2      3    
Dates                                                                                                                              ...
2018-01-01            Golf    Van    Jeep      Golf1    Van1    Jeep1
2018-01-02            Golf    Van    Jeep      Golf2    Van2    Jeep2

EDIT: Now if I wanted to relabel all the Number index values - how would I create this:

Type                          Type1                    Type2
Numbers                  p1     p2     p3         p1      p2      p3    
Dates                                                                                                                              ...
2018-01-01            Golf    Van    Jeep      Golf1    Van1    Jeep1
2018-01-02            Golf    Van    Jeep      Golf2    Van2    Jeep2

EDIT: Can just use: .add_prefix('hh')

like image 412
Bob Avatar asked May 13 '26 09:05

Bob


1 Answers

Use DataFrame.set_index with DataFrame.unstack, then change order of levels by DataFrame.swaplevel and sorting MultiIndex by DataFrame.sort_index:

df = df.set_index(['Dates','Type']).unstack().swaplevel(0,1, axis=1).sort_index(axis=1)
print (df)
Type       Type1             Type2             
               1    2     3      1     2      3
Dates                                          
2018-01-01  Golf  Van  Jeep  Golf1  Van1  Jeep1
2018-01-02  Golf  Van  Jeep  Golf2  Van2  Jeep2

EDIT: Add DataFrame.rename_axis by tuple:

df = (df.set_index(['Dates','Type'])
        .unstack()
        .swaplevel(0,1, axis=1)
        .sort_index(axis=1)
        .rename_axis(('Type','Numbers'), axis=1))
print (df)
Type       Type1             Type2             
Numbers        1    2     3      1     2      3
Dates                                          
2018-01-01  Golf  Van  Jeep  Golf1  Van1  Jeep1
2018-01-02  Golf  Van  Jeep  Golf2  Van2  Jeep2
like image 105
jezrael Avatar answered May 15 '26 00:05

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!