Keep other columns when using sum() with groupby

Given a pandas dataframe, we have to keep other columns when using sum() with groupby. By Pranit Sharma Last updated : October 03, 2023

Prerequisite

  • Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
  • The groupby() method is a simple but very useful concept in pandas. By using groupby, we can create a grouping of certain values and perform some operations on those values. The groupby() method split the object, apply some operations, and then combines them to create a group hence large amounts of data and computations can be performed on these groups.

Problem statement

What we mean by this is suppose we are given a dataframe that contains a column called name and some columns contain some value. For the same name we need grouped sum of each value column.

How to keep other columns when using sum() with groupby?

One of the simplest methods on groupby objects is the sum() method, here we will apply groupby method along with an agg method inside which we will pass a dictionary where the keys will represent the column names and values will represent the agg() function that has to be applied in them.

Let us understand with the help of an example,

Python program to keep other columns when using sum() with groupby

# Importing pandas package
import pandas as pd

# Creating a dictionary
d = {
    'name':['Ram','Shyam','Shyam','Ram'],
    'value1':[1,1,2,1],
    'value2':[2,0,0,2],
    'age':[20,21,21,20]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# grouping the column name and assigning the value 
# column with their new values
res = df.groupby(['name'], as_index=False).agg({'value1': 'sum', 'value2': 'sum', 'age': 'first'})

# Display Result
print("Result:\n",res)

Output

The output of the above program will be:

Example: Keep other columns when using sum() with groupby

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.