0

So, I have the following dataframe in Pandas:

Name Sport Age
Mary Soccer 20
Ronald Basketball 31
Mary Rugby 20
Ronald Tennis 31
Ronald Golf 31
Jennifer Tennis 18
Jennifer Soccer 18

And just like I could do in SQL, I need to isolate the unique names the unique sports. However, each unique sport must become a column, so I can display how many times it appeared, like this:

Name Soccer Basketball Rugby Tennis Golf
Mary 1 0 1 0 0
Ronald 0 1 0 1 1
Jennifer 1 0 0 1 0

I'm completely lost and don't know how to solve that. Could someone help me, please?

  • 2
    this is just a pivot/crosstab `pd.crosstab(df['Name'],df['Sport'])` ? Unless i'm missing something – Umar.H Mar 09 '21 at 20:30
  • 1
    Jesus man, thank you so, so much. I spent more than 3 hours trying to solve this problem and you gave me an answer only a few minutes after I posted! That's exactly what I was looking for, thank you! – Marcos Dias Mar 09 '21 at 20:40

0 Answers0