TransWikia.com

How to combine rows after Pandas Groupby function

Data Science Asked on November 14, 2020

May I know how to combine several rows into one single row after I used Pandas groupby function?

In below example, I would like to to group the data by Employee ID, Customer Last Name and Customer First Name. Then I want all his dependents’ data listed in the same row.

Thanks a lot!

enter image description here

enter image description here

One Answer

You should use the function drop_duplicates :

Define the columns you take into account:

unique_columns = Seq("Employee Id", "Customer Last name", "Customer First Name")

But it seems to me that the column Employee Id is enough, as you have only one person per Employee Id so you can define :

unique_columns = Seq("Employee Id", "Customer Last name", "Customer First Name")

Then you can write:


import pandas as pd 

unique_columns = Seq("Employee Id", "Customer Last name", "Customer First Name")
df = df.drop_duplicates(subset=unique_columns)

Answered by Catalina Chircu on November 14, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP