Hello,
I have a Pandas DataFrame df
like this:
name value
2014-01-01 temp 10.0
2014-01-01 p 101.0
2014-01-02 temp 11.5
2014-01-02 p 100.0
I wanted to get a new DataFrame df2
like:
temp p
2014-01-01 10.0 101.0
2014-01-02 11.5 100.0
I did this:
df2 = df.pivot(columns='name', values='value')
but it raises cannot label index with a null key
I need to do this
df2 = df.pivot(index=df.index, columns='name', values='value')
see http://stackoverflow.com/questions/26101246/spread-data-across-multiple-columns
In my mind, passing index parameter shouldn't be necessary when applying pivot function in such a case.
Kind regards
Comment From: jreback
The canonical way of doing this is as follows:
In [25]: df.reset_index().pivot('index','name','value')
Out[25]:
name p temp
index
2014-01-01 101 10.0
2014-01-02 100 11.5
That said, I think it could simply use the index (though their IS a bit of ambiguity as even in this simple case (after the reset) its an integer index).
pull requests are welcome for an enhancement like this.
Comment From: TomAugspurger
This seems to have a different behavior now.
In [38]: df.pivot_table(columns='name', values='value')
Out[38]:
name p temp
value 100.5 10.75
Closing, since we won't break compatibility over this.