Home > Enterprise >  How to add Levels to Column Index?
How to add Levels to Column Index?

Time:12-02

I want to add levels to a Column Index in a dataframe. How can I do it?

I have a pandas dataframe like this:

dfa = pd.DataFrame.from_dict({'Escola': {0: 'Brasília', 1: 'Brasília', 2: 'São Luis', 3: 'PIO XII'},
                             'Série': {0: 'EF2', 1: 'EF8', 2: 'BER1', 3: 'EI5'},
                             'Tipo': {0: 'Rematrícula', 1: 'Rematrícula', 2: 'Nova', 3: 'Nova'},
                             'Matrícula': {0: 0.0, 1: 0.0, 2: 0.0, 3: 2.0},
                             'Meta': {0: 164, 1: 176, 2: 9, 3: 12},
                             '%': {0: 0.0, 1: 0.0, 2: 0.0, 3: 16.7},
                             'Média Histórica': {0: np.nan, 1: 13, 2: np.nan, 3: 5},
                             'Matrícula Projetada': {0: 0, 1: 13, 2: 0, 3: 7},
                             'Meta Total': {0: 164, 1: 176, 2: 9, 3: 23},
                             '%P': {0: 0.0, 1: 7.4, 2: 0.0, 3: 30.4},
                             '% Meta': {0: 0.0, 1: 7.4, 2: 0.0, 3: 30.4},
                             '# Alunos que Faltam': {0: 164, 1: 163, 2: 9, 3: 16}})
dfa
     Escola Série         Tipo  ...    %P  % Meta  # Alunos que Faltam
0  Brasília   EF2  Rematrícula  ...   0.0     0.0                  164
1  Brasília   EF8  Rematrícula  ...   7.4     7.4                  163
2  São Luis  BER1         Nova  ...   0.0     0.0                    9
3   PIO XII   EI5         Nova  ...  30.4    30.4                   16

And I want to add a top row over the column names:

enter image description here

CodePudding user response:

You can make tuples as you wish and use pd.MultiIndex.from_tuples. Without repeating all the column names, just knowing that you would like to group the existing columns in groups of (3, 3, 4, 2):

dfa.columns = pd.MultiIndex.from_tuples(zip(
    [''] * 3   ['Name1'] * 3   ['Name2'] * 4   ['Name3'] * 2,
    dfa.columns,
))

The outcome is:

>>> dfa
                                   Name1                      Name2  \
     Escola Série         Tipo Matrícula Meta     % Média Histórica   
0  Brasília   EF2  Rematrícula       0.0  164   0.0             NaN   
1  Brasília   EF8  Rematrícula       0.0  176   0.0            13.0   
2  São Luis  BER1         Nova       0.0    9   0.0             NaN   
3   PIO XII   EI5         Nova       2.0   12  16.7             5.0   

                                        Name3                      
  Matrícula Projetada Meta Total    %P % Meta # Alunos que Faltam  
0                   0        164   0.0    0.0                 164  
1                  13        176   7.4    7.4                 163  
2                   0          9   0.0    0.0                   9  
3                   7         23  30.4   30.4                  16  
  • Related