Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Have multiple names in a column, need to create a row for each one

I have a dataframe in which I have a column with multiple values (column A), I need to create a row for each value in this column keeping the data from the other columns (column B and others)

Column A Column B
Xbox, Playstation Xbox Elite 2, Dualsense Edge
Xbox, Nintendo, Atlus Persona 3 Portable, Persona 4 Golden

I’m expecting something like this

Column A Column B
Xbox Xbox Elite 2, Dualsense Edge
Playstation Xbox Elite 2, Dualsense Edge
Xbox Persona 3 Portable, Persona 4 Golden
Nintendo Persona 3 Portable, Persona 4 Golden
Atlus Persona 3 Portable, Persona 4 Golden

I know this can be done manually, but I want to know if there is a way to do it with python, I have around 400 rows

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

here is one way to do it, convert the values to a list and then explode into rows

# split the column on , which makes the content of columnA as a list
df['Column A']=df['Column A'].str.strip().str.split(',' )

# explode
out=df.explode(['Column A'])
out

    Column A        Column B
0   Xbox            Xbox Elite 2, Dualsense Edge
0   Playstation     Xbox Elite 2, Dualsense Edge
1   Xbox            Persona 3 Portable, Persona 4 Golden
1   Nintendo        Persona 3 Portable, Persona 4 Golden
1   Atlus           Persona 3 Portable, Persona 4 Golden
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading