1

I would like to combine two columns for index while pivoting a pandas dataframe. I'm using the following code to do so:

ConceptTemp = Concept.pivot(index=['memberid','testscoreid'], columns='questionid', values='correct')

this gives me the following error:

ValueError: Wrong number of items passed 1532, placement implies 2

1532 is the number of rows in my dataframe. I can't pivot only on memberid or on testscoreid as I'll have duplicate questionid values. The index column has to be a combination of testscoreid AND memberid.

Would anyone have any pointers on how to get this done?

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
Patthebug
  • 4,647
  • 11
  • 50
  • 91

1 Answers1

2

I think you can use pivot_table:

ConceptTemp = Concept.pivot_table(index=['memberid','testscoreid'],
                                  columns='questionid', 
                                  values='correct')

pivot_table uses aggfunc, default is aggfunc=np.mean if duplicates. Better explanation with sample is here and in docs.

Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252