0

I have a dataframe which is like below.

     ATTR1  ATTR2  ATTR3  UnitType    Value    Date
  0  1       2      3     TotalSales  10       10/2020
  1  1       2      3     Canceled    1        10/2020
  2  1       2      3     Returned    3        10/2020
  3  3       2      1     TotalSales  10       10/2020
  4  3       2      1     Canceled    1        10/2020
  5  3       2      1     Returned    3        10/2020

Data continues with different months. I want to make Date only 1 row for each attr1-2-3 combination and make new columns for each unit type.

I tried pivot function like below but it didn't work.

data.pivot(index=['Date', 'ATTR1', 'ATTR2', 'ATTR3'], columns='UnitType', values='Value')

It gives the following error.

Length of passed values is 1346544, index implies 4
cs95
  • 379,657
  • 97
  • 704
  • 746
ali srn
  • 573
  • 5
  • 24

1 Answers1

0

The thing is, there were already index columns in my dataframe and pivot indexes are not the same for my case. I was using different columns while melting and using extra column while pivoting. So, I needed to use set_index() and set my pivot parameters as index. Then it worked.

ali srn
  • 573
  • 5
  • 24