How to use pivot to get 2 columns into a column multiindex in pandas

Viewed 98

I have a data frame with 4 columns (a, b, c, d are column names):

df = 
a   b   c    d
1   2   3    4
5   2   7    8

Is it possible to use df.pivot() to get 2 columns into the column multiindex? The following doesn't work:

df.pivot('a', ['b', 'c'])

I want

b  2
c  3   7
a  
1  4   NA
5  NA  8

I know I can use pivot_table to get this done easily (pd.pivot_table(df, index='a', columns=['b', 'c'])) but I'm curious about the flexibility of pivot as the documentation isn't clear.

3 Answers
Related