0

I have a dataframe with information of different users (ID) with many duplicated categorical variables (photo) and its corresponding numbers of interactions (likes). How i can calculate the sum of total likes for each different photo type?

For example:

id    photo_type    likes 
1     nature          2
2     art             4
3     art             1
4     fashion         3
5     fashion         2

I expect to get information like that:

total numbers of likes for nature:2
total numbers of likes for art: 5
total numbers of likes for fashion: 5
Chris
  • 29,127
  • 3
  • 28
  • 51

1 Answers1

1

Use pandas.DataFrame.groupby:

df.groupby('photo_type')['likes'].sum()

Output:

photo_type
art        5
fashion    5
nature     2
Name: likes, dtype: int64
Chris
  • 29,127
  • 3
  • 28
  • 51