0

I currently have a file in a format similar to the following

Person Circumstance Outcome
Person A X 1
Person A Y 2
Person A X 3
Person A Y 4
Person A X 1
Person B Y 2
Person B X 3
Person B Y 4
Person C X 1
Person C Y 2

I am trying to write something that will take that data and produce something like

Person Circumstance Count of Outcome 1 Count of Outcome 2 Count of Outcome 3 Count of Outcome 4
Person A X 2 0 1 0
Person A Y 0 1 0 1
Person B X 0 0 1 0
Person B Y 0 1 0 1
Person C X 1 0 0 0
Person C Y 0 1 0 0

I'm very much a python novice; I can GroupBy "Person"/"Circumstance" alright but am ending up with gibberish in the "outcome" fields. I'm struggling to conceptualize how to go about creating a dataframe that separately tracks a count of each "Outcome" field associated with a Person/Circumstance combination.

Any guidance is appreciated, thank you everybody

kpdawson24
  • 23
  • 4
  • 2
    Use `pd.crosstab([df.Person, df.Circumstance], df.Outcome).add_prefix('Count of Outcome ').reset_index()` – jezrael Nov 04 '22 at 06:29

0 Answers0