0
votes

I have csv data that I flattened from JSON. The problem is, there are now multiple rows with the same primary key, that have columns with different values. For example:

What I have:

Name, Age, Position, AddressLine1, AddressLine2, AddressLine3
JohnSmith, 42, Manager, 3430, Street Ln, Suite 4
JohnSmith, 42, Manager, 2451, Address Ln, Suite 6
JohnSmith, 42, Manager, 2451, Address Ln, Suite 7
SantaClause, 56, Partner, 2463, Northpole Dr, Apt 430
SantaClause, 56, Manager, 2463, Northpole Dr, Apt 430

What I want:

Name, Age, Position, Position_2, AddressLine1, AddressLine1_2, AddressLine2, AddressLine2_2 AddressLine3, AddressLine3_2 AddressLine3_3, AddressLine3_4
JohnSmith, 42, Manager,, 3430, 2451, Street Ln, Address Ln, Suite 4, Suite 6, Suite 7
SantaClause, 56, Partner, Manager, 2463,, Northpole Dr,, Apt 430,,

Is there a way to do this in python that will merge the duplicate rows based off of a key column and then create new columns when multiple distinct values are found?

the date is represented in a dict?eran halperin
@eranhalperin the data is currently in a CSV filetechconsulted1012
do you know how to use pandas to get the CSV data into a pandas dataframe?eran halperin