Python Pandas group by, values spread across multiple columns


This Content is from Stack Overflow. Question asked by user3104376

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?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?