In [2]:
df
Out[2]:
date variable value
0 2000-01-03 A 0.469112
1 2000-01-04 A -0.282863
2 2000-01-05 A -1.509059
3 2000-01-03 B -1.135632
4 2000-01-04 B 1.212112
5 2000-01-05 B -0.173215
6 2000-01-03 C 0.119209
7 2000-01-04 C -1.044236
8 2000-01-05 C -0.861849
9 2000-01-03 D -2.104569
10 2000-01-04 D -0.494929
11 2000-01-05 D 1.071804
In [3]:
df.pivot(index='date', columns='variable', values='value')
Out[3]:
variable A B C D
date
2000-01-03 0.469112 -1.135632 0.119209 -2.104569
2000-01-04 -0.282863 1.212112 -1.044236 -0.494929
2000-01-05 -1.509059 -0.173215 -0.861849 1.071804
In [4]:
df['value2'] = df['value'] * 2
df
Out[4]:
date variable value value2
0 2000-01-03 A 0.469112 0.938224
1 2000-01-04 A -0.282863 -0.565726
2 2000-01-05 A -1.509059 -3.018118
3 2000-01-03 B -1.135632 -2.271264
4 2000-01-04 B 1.212112 2.424224
5 2000-01-05 B -0.173215 -0.346430
6 2000-01-03 C 0.119209 0.238418
7 2000-01-04 C -1.044236 -2.088472
8 2000-01-05 C -0.861849 -1.723698
9 2000-01-03 D -2.104569 -4.209138
10 2000-01-04 D -0.494929 -0.989858
11 2000-01-05 D 1.071804 2.143608

Pivot

In [5]:
df.pivot(index='date', columns='variable', values='value')
Out[5]:
variable A B C D
date
2000-01-03 0.469112 -1.135632 0.119209 -2.104569
2000-01-04 -0.282863 1.212112 -1.044236 -0.494929
2000-01-05 -1.509059 -0.173215 -0.861849 1.071804

Hierarchical columns

In [6]:
pivoted = df.pivot(index='date', columns='variable')
pivoted
Out[6]:
value value2
variable A B C D A B C D
date
2000-01-03 0.469112 -1.135632 0.119209 -2.104569 0.938224 -2.271264 0.238418 -4.209138
2000-01-04 -0.282863 1.212112 -1.044236 -0.494929 -0.565726 2.424224 -2.088472 -0.989858
2000-01-05 -1.509059 -0.173215 -0.861849 1.071804 -3.018118 -0.346430 -1.723698 2.143608
In [7]:
pivoted['value']
Out[7]:
variable A B C D
date
2000-01-03 0.469112 -1.135632 0.119209 -2.104569
2000-01-04 -0.282863 1.212112 -1.044236 -0.494929
2000-01-05 -1.509059 -0.173215 -0.861849 1.071804