Skip to content
Advertisement

Transpose and Groupby pandas Columns

I’m looking to transpose pandas columns and apply a Groupby

JavaScript

How can I achieve this expected output ?

JavaScript

Advertisement

Answer

Use DataFrame.stack for reshape with remove missing values and count values by Series.value_counts, last Series.sort_index with Series.rename_axis and Series.reset_index for 2 columns DataFrame:

JavaScript

EDIT: Use DataFrame.melt and then aggregate by size and join in GroupBy.agg:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement