Skip to content Skip to sidebar Skip to footer

Pivoting A One-hot-encode Dataframe

I have a pandas dataframe that looks like this: genres.head() Drama Comedy Action Crime Romance Thriller Adventure Horror Mystery Fantasy ... History Music War Do

Solution 1:

Maybe I'm missing something but doesn't this work for you?

agg = df.groupby('number_of_genres').agg('sum').T
agg['totals'] = agg.sum(axis=1)

Edit: Solution via pivot_table

agg = df.pivot_table(columns='number_of_genres', aggfunc='sum')
agg['total'] = agg.sum(axis=1)

Post a Comment for "Pivoting A One-hot-encode Dataframe"