Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'fff': ['one', 'one', 'one', 'two', 'two',
                           'two'],
                   'bbb': ['P', 'Q', 'R', 'P', 'Q', 'R'],
                   'baa': [2, 3, 4, 5, 6, 7],
                   'zzz': ['h', 'i', 'j', 'k', 'l', 'm']})
df
Out[2]:
fff bbb baa zzz
0 one P 2 h
1 one Q 3 i
2 one R 4 j
3 two P 5 k
4 two Q 6 l
5 two R 7 m

Pandas: Dataframe - pivot.

In [3]:
df.pivot(index='fff', columns='bbb', values='baa')
Out[3]:
bbb P Q R
fff
one 2 3 4
two 5 6 7

Pandas: Dataframe - pivot with index, column and value.

In [4]:
df.pivot(index='fff', columns='bbb')['baa']
Out[4]:
bbb P Q R
fff
one 2 3 4
two 5 6 7
In [5]:
df.pivot(index='fff', columns='bbb', values=['baa', 'zzz'])
Out[5]:
baa zzz
bbb P Q R P Q R
fff
one 2 3 4 h i j
two 5 6 7 k l m

Pandas: Dataframe - pivot with index, column and value.