w3resource

GroupBy and create a new column with Aggregated data in Pandas


13. 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.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to group data and then create a new column that contains the group mean for each row.
  • Write a Pandas program to group a DataFrame and merge aggregated totals back into the original DataFrame as a new column.
  • Write a Pandas program to group data and add a column showing the count of records in each group next to each original row.
  • Write a Pandas program to group data and compute a new column that represents the percentage contribution of each record to its group's total.

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.



Follow us on Facebook and Twitter for latest update.