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

Time:11-04

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

CodePudding user response:

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
  • Related