0

I have a dataframe with several different columns where I would like to sum the counts of where a df[x] = df[y].

So to make an example of what I'm trying to do, I'll use two example columns.

Imagine a DF with a column FAVORITE_COLORS and a column COUNTRY.

I would like to sum all the different combinations of these, I.e how many times England has the favorite color "Blue", "Yellow" etc. and store this in a df.

So my desired output would be

    COUNTRY        BLUE        YELLOW     COLOR...
0  England         1232        3312       Sum
1  Hungary         5543        7661       Sum
2  ETC             ETC         ETC

I tried using the pandas groupby and count, using pythons len(df) when I created a new df containing only the matches, but it seemed very cumbersome and I couldn't get it to work properly.

Any thoughts on how to approach this?

Thanks in advance

zagzig
  • 69
  • 1
  • 5

0 Answers0