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.

Adding column to groupby dataframe

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.

For this purpose, 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 code 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 »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.