Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Multi-Column from_tuples?

I get how to use pd.MultiIndex.from_tuples() in order to change something like

       Value
(A,a)  1
(B,a)  2
(B,b)  3

into

                Value
Caps Lower      
A    a          1
B    a          2
B    b          3

But how do I change column tuples in the form

       (A, a)  (A, b) (B,a)  (B,b)
index
1      1       2      2      3
2      2       3      3      2
3      3       4      4      1

into the form

 Caps         A              B
 Lower        a       b      a      b
 index
 1            1       2      2      3
 2            2       3      3      2
 3            3       4      4      1

Many thanks.


Edit: The reason I have a tuple column header is that when I joined a DataFrame with a single level column onto a DataFrame with a Multi-Level column it turned the Multi-Column into a tuple of strings format and left the single level as single string.


Edit 2 - Alternate Solution: As stated the problem here arose via a join with differing column level size. This meant the Multi-Column was reduced to a tuple of strings. The get around this issue, prior to the join I used df.columns = [('col_level_0','col_level_1','col_level_2')] for the DataFrame I wished to join.

like image 741
Little Bobby Tables Avatar asked Jun 15 '16 12:06

Little Bobby Tables


People also ask

How do I create a multi-level column in pandas?

Tested in versions of pandas as early as 0.22. 0 . Simply create a DataFrame (ignore columns in the first step) and then set colums equal to your n-dim list of column names. Save this answer.


2 Answers

Assign direct to columns with the result from pd.MultiIndex.from_tuples passing in your existing columns:

In [186]:
l=[('A', 'a'),  ('A', 'b'), ('B','a'),  ('B','b')]
df = pd.DataFrame(np.random.randn(5,4), columns = l)
df

Out[186]:
     (A, a)    (A, b)    (B, a)    (B, b)
0 -0.876353  0.553742  1.631858 -0.561309
1  0.463058 -0.455014 -0.491336 -1.436059
2  0.337810  0.233624 -0.571749 -2.259763
3  1.073057 -0.475894  0.999643 -0.379743
4  0.441800  0.311202 -0.191552  0.291268

In [187]:    
df.columns = pd.MultiIndex.from_tuples(df.columns, names=['Caps','Lower'])
df

Out[187]:
Caps          A                   B          
Lower         a         b         a         b
0     -0.876353  0.553742  1.631858 -0.561309
1      0.463058 -0.455014 -0.491336 -1.436059
2      0.337810  0.233624 -0.571749 -2.259763
3      1.073057 -0.475894  0.999643 -0.379743
4      0.441800  0.311202 -0.191552  0.291268

note that you can assign directly to names attribute of the columns attribute like the following:

df.columns.names = ['Caps','Lower']

not to be confused with the name attribute

like image 106
EdChum Avatar answered Sep 20 '22 16:09

EdChum


Another solution is use MultiIndex.from_tuples with parameter names:

import pandas as pd

df = pd.DataFrame({'Value': [1,2,3]}, index=[('A','a'),('B','a'),('B','b')])
print (df)
        Value
(A, a)      1
(B, a)      2
(B, b)      3

df.index = pd.MultiIndex.from_tuples(df.index, names=['Caps','Lower'])
print (df)
            Value
Caps Lower       
A    a          1
B    a          2
     b          3

This same works with columns, see Edchum's answer:

df.columns= pd.MultiIndex.from_tuples(df.columns, names=['Caps','Lower'])
like image 32
jezrael Avatar answered Sep 19 '22 16:09

jezrael