Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing index parameter shouldn't be necessary when applying pivot function #8436

Closed
scls19fr opened this issue Oct 1, 2014 · 2 comments
Closed
Labels
Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@scls19fr
Copy link
Contributor

scls19fr commented Oct 1, 2014

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 df2like:

            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

@jreback
Copy link
Contributor

jreback commented Oct 1, 2014

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.

@jreback jreback added Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Oct 1, 2014
@jreback jreback modified the milestones: 0.16, 0.15.1 Oct 1, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 6, 2015
@TomAugspurger
Copy link
Contributor

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.

@TomAugspurger TomAugspurger modified the milestones: Contributions Welcome, No action Jul 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

3 participants