GroupBy and create a new column with Aggregated data in Pandas
Pandas Advanced Grouping and Aggregation: Exercise-13 with Solution
GroupBy and Create a New Column with Aggregated Data:
Write a Pandas program to create a new column with aggregated data from a GroupBy operation for enriched data insights.
Sample Solution:
Python Code :
import pandas as pd
# Sample DataFrame
data = {'Category': ['A', 'A', 'B', 'B', 'C', 'C'],
'Value': [10, 20, 30, 40, 50, 60]}
df = pd.DataFrame(data)
print("Sample DataFrame:")
print(df)
# Group by 'Category' and calculate the sum
print("\nGroup by 'Category' and calculate the sum:")
df['SumValue'] = df.groupby('Category')['Value'].transform('sum')
print(df)
Output:
Sample DataFrame: Category Value 0 A 10 1 A 20 2 B 30 3 B 40 4 C 50 5 C 60 Group by 'Category' and calculate the sum: Category Value SumValue 0 A 10 30 1 A 20 30 2 B 30 70 3 B 40 70 4 C 50 110 5 C 60 110
Explanation:
- Import pandas.
- Create a sample DataFrame.
- Group by 'Category' and calculate the sum.
- Create a new column 'SumValue' with the aggregated data.
- Print the DataFrame with the new column.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: GroupBy and Apply different functions using a Dictionary in Pandas.
Next: GroupBy and Handle Missing data in Pandas.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics