0

So, I have a dataset:

foo  bar  baz
---  ---  ---
one   A    1
one   B    2
one   B    3
two   A    4
two   B    5
two   C    6

I want to get such table:

foo  A  B  C
---  -  -  -
one  1  5  0
two  4  5  6

And by using:

df.pivot(index='foo', columns='bar', values='baz')

I am getting such error:

ValueError: Index contains duplicate entries, cannot reshape

How I can sum duplicate elements in pivot? Or maybe there is another command for such manipulations?

1 Answers1

1

Use pivot_table instead:

>>> df.pivot_table("baz", "foo", "bar", "sum").fillna(0)

bar    A    B    C
foo               
one  1.0  5.0  0.0
two  4.0  5.0  6.0
not_speshal
  • 22,093
  • 2
  • 15
  • 30