Python Pandas group by, values spread across multiple columns

Viewed 23

I have a database of blogposts. Each blog post is a row and has 4 columns: views and 3 columns with a category. I want to have the average views of blog posts across a certain category. However, a specfic category could be in any of 3 columns.

Currently I have this, but this only works on 1 column:

avg_views = data.groupby('category', as_index = False['views'].mean().sort_values('views', ascending = False)

how to group across cat_1, cat_2 and cat_3?

0 Answers
Related