# How to combine rows after Pandas Groupby function

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!

• Could you provide data to reproduce your example? – imitusov Oct 21 at 9:05

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)