0

I am learning pivots and pivot tables in pandas. Below is a practice code I got from a video.

df = pd.DataFrame({'foo': ['one', 'one', 'one','two','two','two'],
'bar': ['A','B','C','A','B','C'],
'baz': [1,2,3,4,5,6],'zoo':['x','y','z','q','w','t']})

df.pivot('foo', 'bar', 'baz')

When I try apply this to my personal data I get the follwoing error:

ValueError: Index contains duplicate entries, cannot reshape

This is confusing because the practice code also has duplicate indexes, but I don't an error.

Can someone explain to me why?

  • 2
    Okay, when you pivot your dataframe here, you have only one value for each row ('foo') with column('bar'). Therefore this test data pivots. But if you add a row to your dataframe where foo='One', Bar='A', baz='2', and zoo doesn't matter, you will get duplicate entries ValueError. You need to aggregate your values some how. Either use first, mean, sum or some other sort of aggregration function. And to do that you can use either `groupby` or `pivot_table`. – Scott Boston Apr 23 '22 at 18:43
  • 2
    Look at this post https://stackoverflow.com/q/47152691/6361531 – Scott Boston Apr 23 '22 at 18:44
  • 1
    When you call `df.pivot(index, cols, values)` every `index, cols` combination must be unique. In other words, they can only have 1 `values` – Code Different Apr 24 '22 at 02:00

0 Answers0