Pandas add column to groupby dataframe

Given a pandas dataframe, we have to add column to groupby dataframe.
Submitted by Pranit Sharma, on November 09, 2022

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.

Problem statement

We are given a DataFrame with more than one column, our goal is to count values of each type for one column and then add a column with the size of that column.

Adding column to groupby dataframe

For adding column to groupby dataframe, we will first use the groupby operation on our dataframe where we will pass that particular column which we will group and that column with which we will group.

After grouping the columns, we will count the values of this object using the value_counts() method and apply size transformation to add a new column.

Let us understand with the help of an example,

Python program to add column to groupby dataframe

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating a dictionary
d = {
    'A':[1,1,1,2,2,2,2],
    'B':['p','q','o','p','p','q','q']
}

# Creating a DataFrame
df = pd.DataFrame(d)

# Display dataframe
print('Original DataFrame:\n',df,'\n')

# Using groupby
gp = df.groupby('A')['B'].value_counts().reset_index(name='C')

# Adding new column
df['new'] = df.groupby('A')['B'].transform('size')

# Display result
print('Result:\n',df,'\n')

Output

Example: Pandas add column to groupby dataframe

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





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