World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
How do I compute percentages using groupby in Pandas?
To compute percentages by utilizing groupby in Pandas, you can follow these steps:
Here's an example code snippet:
import pandas as pd # read in the dataset df = pd.read_csv('my_dataset.csv') # group the data by a column grouped_data = df.groupby('column_name') # compute the sum or count for each group total_counts = grouped_data['count_column'].sum() # compute the percentage for each group percentages = grouped_data.apply(lambda x: x['count_column'] / total_counts[x.name] * 100) # assign the percentages to a new column df = df.assign(percentage_column=percentages)
In this example, the code first groups the data by a column called "column_name". Then, it computes the sum of a count column for each group using the sum() method. Next, it computes the percentage for each group by dividing the count column by the total count for each group using a lambda function and the apply() method. Finally, it assigns the resulting percentages to a new column called "percentage_column" in the original DataFrame using the assign() method.