warrenfitzhenry
warrenfitzhenry

Reputation: 2299

Pandas Dataframe - multilevel index /column

I am having some difficulty in organizing my dataframe. I think it's pretty simple, but I have been stuck with this for too long:

This is df1:

     Output Energy, (Wh/h)  Lights (Wh)  Lights+Media (Wh)  Total Usage (h)  \
Hour                                                                           
1                       0.0          0.0                0.0              0.0   
2                       0.0          0.0                0.0              0.0   
3                       0.0          0.0                0.0              0.0   
4                       0.0          0.0                0.0              0.0   
5                       0.0          0.0                0.0              0.0   

I'd prefer this to be transposed for ease of use:

df2 =df1.T

gives me:

    Hour                     1    2    3    4   
    Output Energy, (Wh/h)  0.0  0.0  0.0  0.0     
    Lights (Wh)            0.0  0.0  0.0  0.0     
    Lights+Media (Wh)      0.0  0.0  0.0  0.0     
    Total Usage (h)        0.0  0.0  0.0  0.0    
    Lights (h)             0.0  0.0  0.0  0.0  
    Light+Media (h)        0.0  0.0  0.0  0.0  

But ultimately, I would like it to look like this:

                                       Hour
                                  1    2    3    4   
          Output Energy, (Wh/h)  0.0  0.0  0.0  0.0     
          Lights (Wh)            0.0  0.0  0.0  0.0     
CU-101    Lights+Media (Wh)      0.0  0.0  0.0  0.0     
          Total Usage (h)        0.0  0.0  0.0  0.0    
          Lights (h)             0.0  0.0  0.0  0.0  
          Light+Media (h)        0.0  0.0  0.0  0.0  

I have been trying to add the 'Cu-101' as a multilevel column, but to no avail. Should I add this before or after it is transposed?

Also, moving the 'Hour' - I set this column as the index, but how do I move it to a new level?

Upvotes: 3

Views: 1709

Answers (2)

piRSquared
piRSquared

Reputation: 294218

@jezrael's answer is the correct way of doing it and the way I'd do it if I was writing code someone else was going to read.

But here are some creative quick and dirty ways

1

df = df.T
df.index = [['CU-101'] * len(df), df.index]
df.columns = [['Hour'] * len(df.columns), df.columns]

2

pd.concat([pd.concat([df.T], keys=['CU-101'])], axis=1, keys=['Hour'])

Upvotes: 2

jezrael
jezrael

Reputation: 862406

You can use MultiIndex.from_arrays:

df.index = pd.MultiIndex.from_arrays([['Hour'] * len(df.index),
                                      df.index], 
                                      names=(None,None))
df.columns = pd.MultiIndex.from_arrays([['CU-101'] * len(df.columns),
                                        df.columns], 
                                        names=(None,None))

print (df.T)
                             Hour                    
                                1    2    3    4    5
CU-101 Output Energy, (Wh/h)  0.0  0.0  0.0  0.0  0.0
       Lights (Wh)            0.0  0.0  0.0  0.0  0.0
       Lights+Media (Wh)      0.0  0.0  0.0  0.0  0.0
       Total Usage (h)        0.0  0.0  0.0  0.0  0.0

Upvotes: 5

Related Questions