Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas pivot warning about repeated entries on index

Tags:

python

pandas

On Pandas documentation of the pivot method, we have:

Examples -------- >>> df     foo   bar  baz 0   one   A    1. 1   one   B    2. 2   one   C    3. 3   two   A    4. 4   two   B    5. 5   two   C    6.  >>> df.pivot('foo', 'bar', 'baz')      A   B   C one  1   2   3 two  4   5   6 

My DataFrame is structured like this:

   name   id     x ---------------------- 0  john   1      0 1  john   2      0 2  mike   1      1 3  mike   2      0 

And I want something like this:

      1    2   # (this is the id as columns) ---------------------- mike  0    0   # (and this is the 'x' as values) john  1    0 

But when I run the pivot method, it is saying:

*** ReshapeError: Index contains duplicate entries, cannot reshape 

Which doesn't makes sense, even in example there are repeated entries on the foo column. I'm using the name column as the index of the pivot, the first argument of the pivot method call.

like image 202
Tarantula Avatar asked Jun 27 '12 17:06

Tarantula


1 Answers

As far as I can tell with updates to pandas, you have to use pivot_table() instead of pivot().

pandas.pivot_table(df,values='count',index='site_id',columns='week') 
like image 76
Alison S Avatar answered Oct 01 '22 11:10

Alison S