Categories
Python Answers

How to get percentage of total with groupby with Python Pandas?

Spread the love

To get percentage of total with groupby with Python Pandas, we can use the / operator to get the percentage with each item in a column.

For instance, we write

df['sales'] / df.groupby('state')['sales'].transform('sum')

to divide the values in the sales column with the sales values grouped by the state value and summed together with transform.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *