0

I have a pandas dataframe that looks like

Event     Person     Data  
Event1    Person1    Data1
Event1    Person2    Data2
Event1    Person3    Data3
Event2    Person1    Data4
Event2    Person2    Data5
Event2    Person3    Data6

and so on. I would like to create a new dataframe where the rows are the people (removing duplicates) and the columns are the events, and each cell is the particular datapoint corresponding to that event and that person. I can obviously just create a new dataframe and populate it iterating through the rows and columns of that dataframe, but I'm wondering if there's a slicker way?

1 Answers1

1

Use df.pivot()

df.pivot(columns='Event', values='data', index='Person')

Output:

Event     Event1   Event2
Person      
Person1   Data1    Data4
Person2   Data2    Data5
Person3   Data3    Data6
Sociopath
  • 13,068
  • 19
  • 47
  • 75