Closed
Description
Creating a pivot table from a DataFrame is easy.
In [17]: df
Out[17]:
date variable value
0 2000-01-03 00:00:00 A 0.528219
1 2000-01-04 00:00:00 A -0.135071
2 2000-01-05 00:00:00 A -0.343018
3 2000-01-03 00:00:00 B -0.097701
4 2000-01-04 00:00:00 B -1.383248
In [18]: df.pivot('date', 'variable', 'value')
Out[18]:
variable A B
date
2000-01-03 0.528219 -0.097701
2000-01-04 -0.135071 -1.383248
2000-01-05 -0.343018 NaN
However if the index has been set, and there is no column to be set as index, pivot fails. From api point of view, the index
argument is optional, but in fact it is not.
In [19]: df.set_index('date', inplace=True)
In [20]: df
Out[20]:
variable value
date
2000-01-03 A 0.528219
2000-01-04 A -0.135071
2000-01-05 A -0.343018
2000-01-03 B -0.097701
2000-01-04 B -1.383248
In [21]: df.pivot(columns='variable', values='value')
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
...
KeyError: u'no item named None'
Of course one can reset the index before using pivot.
In [22]: df.reset_index().pivot('date', 'variable', 'value')
Out[22]:
variable A B
date
2000-01-03 0.528219 -0.097701
2000-01-04 -0.135071 -1.383248
2000-01-05 -0.343018 NaN