noobie-php
noobie-php

Reputation: 7243

Pandas merge/faltten dataframe based on specific column

i have following data sample i am trying to flatten it out using pandas, i wanna flatten this data over Candidate_Name.

enter image description here

This is my implementation,

df= df.merge(df,on=('Candidate_Name'))

but i am not getting desired result. My desired output is as follows. So basically have all the rows that match Candidate_Name in a single row, where duplicate column names may suffix with _x

enter image description here

Upvotes: 1

Views: 63

Answers (1)

jezrael
jezrael

Reputation: 863611

I think you need GroupBy.cumcount with DataFrame.unstack and then flatten MultiIndex with same values for first groups and added numbers for another levels for avoid duplicated columns names:

df = df.set_index(['Candidate_Name', df.groupby('Candidate_Name').cumcount()]).unstack()
df.columns = [a if b == 0 else f'{a}_{b}' for a, b in df.columns]

Upvotes: 1

Related Questions