Tarantula Tarantula - 7 months ago 17
Python Question

Pandas pivot warning about repeated entries on index

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.

Answer

Works fine for me? Can you post the exact pivot method call you're using?

In [4]: df.pivot('name', 'id', 'x')
Out[4]: 
id    1  2
name      
john  0  0
mike  1  0