kiran u
kiran u

Reputation: 49

Combine two columns with same name pandas

I have a list from an API response:

list = [[[['3200', 'house_number'],['northline ave', 'road'],['ste 360', 'unit'],['greensboro', 'city'],['27408', 'postcode'],['7611', 'city'],['nc', 'state'],['us', 'country']]]]

enter image description here

As you can see I have column road twice, I want to combine two road columns into one. Actually I want to write a code for, suppose if any column is repaeating any number of times I want to combine and create a new column with same column name like below:

enter image description here

It doesn't matter wheater the road column is reapeating or city column is repeating. Irrespective of column names code should concat or combine or merge two strings with a space delimited.

Thanks again for helping me to do this project. I appreciaate each and everryone who read this question

Regards,

Upvotes: 0

Views: 1915

Answers (1)

Onyambu
Onyambu

Reputation: 79388

You could do:

df.T.reset_index().groupby('index').agg(','.join).T

index             city country house_number  ...           road state     unit
0      greensboro,7611      us         3200  ...  northline ave    nc  ste 

Upvotes: 1

Related Questions