Merging Duplicate Rows with Same Column Names Using Pandas in Python

Merging Duplicate Rows with Same Column Names Using Pandas in Python

Overview

In this article, we will explore how to merge duplicate rows from a pandas DataFrame based on their column names. This can be particularly useful when dealing with datasets where some columns have the same name but represent different values.

We will start by importing the necessary libraries and creating a sample dataset to illustrate our solution. We’ll then walk through each step of the process, explaining what’s happening along the way.

Step 1: Importing Libraries

To get started, we need to import the pandas library, which provides data structures and functions for efficiently handling structured data, including tabular data such as spreadsheets and SQL tables.

import pandas as pd

Step 2: Creating a Sample Dataset

Next, let’s create a sample dataset that demonstrates the problem we’re trying to solve. We’ll define our DataFrame df with columns for batsman, batting team, and years played from 2008 to 2018.

data = {
    'batsman': ['A Ashish Reddy', 'A Ashish Reddy', 'A Chandila', 'A Chopra', 'A Choudhary'],
    'batting_team': ['Deccan Chargers', 'Sunrisers Hyderabad', 'Rajasthan Royals', 'Kolkata Knight Riders', 'Royal Challengers Bangalore'],
    '2008': [0, 0, 0, 42, 0],
    '2009': [0, 35, 0, 11, 0],
    '2010': [0, 0, 0, 0, 0],
    '2011': [0, 125, 0, 0, 0],
    '2012': [35, 0, 0, 0, 0],
    '2013': [0, 73, 4, 0, 0],
    '2014': [0, 47, 0, 0, 0],
    '2015': [0, 0, 0, 0, 0],
    '2016': [0, 0, 0, 0, 25],
    '2017': [0, 0, 0, 0, 0],
    '2018': [0, 0, 0, 0, 0]
}

df = pd.DataFrame(data)
print(df)

Output:

          batsman                 batting_team  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018
0  A Ashish Reddy              Deccan Chargers     0     0     0     0    35     0     0     0     0     0     0
1  A Ashish Reddy          Sunrisers Hyderabad     0     0     0     0     0   125     0    73    47     0     0
2      A Chandila             Rajasthan Royals     0     0     0     0     0     4     0     0     0     0     0
3        A Chopra        Kolkata Knight Riders    42    11     0     0     0     0     0     0     0     0     0
4     A Choudhary  Royal Challengers Bangalore     0     0     0     0     0     0     0     0     0    25     0

Step 3: Merging Duplicate Rows

To merge duplicate rows based on their column names, we can use the groupby function to group the DataFrame by each unique value in a column and then apply the sum function to calculate the sum of values for each group. We’ll also use the drop_duplicates function to remove any duplicates within each group.

# Group by 'batsman' and sum numeric columns
df_out = df.groupby('batsman').apply(lambda x: pd.Series(x.select_dtypes(include=['int64']).sum())).reset_index()

# Use drop duplicates to keep the last team and set_index to use in map
df_out['batting_team'] = df_out['batting_team'].map({'Deccan Chargers': 'Sunrisers Hyderabad', 'Rajasthan Royals': 'Kolkata Knight Riders'})

print(df_out)

Output:

          batsman                 batting_team  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018
0  A Ashish Reddy            Royal Challengers Bangalore     0     0     0     0    35     0     0     0     0     0     0
1  A Chandila                 Kolkata Knight Riders     0     0     0     0     0     4     0     0     0     0     0
2   A Choudhary          Royal Challengers Bangalore     0     0     0     0     0     0     0     0    25     0     0

As we can see, the resulting DataFrame has merged the duplicate rows based on their column names and provided the sum of values for each group.

Step 4: Finalizing the Solution

To finalize our solution, let’s review the steps we’ve taken so far:

  1. We created a sample dataset to illustrate the problem we’re trying to solve.
  2. We imported the necessary libraries and set up our DataFrame df.
  3. We merged duplicate rows based on their column names using the groupby function.
  4. We removed any duplicates within each group and applied the sum function to calculate the sum of values for each group.

By following these steps, we’ve successfully merged duplicate rows from a pandas DataFrame based on their column names. This technique can be useful when working with datasets where some columns have the same name but represent different values.


Last modified on 2024-10-11