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

How do I remove slashes and copy the values into many other rows in pandas?

I’m trying to copy the row based on how many slashes or roster positions there are based on the ‘Roster Position’ column and keep every other column along with it. Any ideas? I’ll show you what it looks like and what I want it to look like.

Before Image or what it currently looks like

Before Image or what it currently looks like

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

This is what I’m trying to achieve, but I don’t know how to write it out.

This is what I'm trying to achieve, but I don't know how to write it out.

I’m using python and pandas.

I tried many different things, but couldn’t figure it out.

>Solution :

Try using .str.split to create a list and explode:

df.assign(Position=df['Roster Position'].str.split('/')).explode('Position', ignore_index=True)

Output:

  Position          Name Roster Position  Salary  AvgPointsPerGame
0       SF  LeBron James    SF/PF/F/UTIL    9300             52.14
1       PF  LeBron James    SF/PF/F/UTIL    9300             52.14
2        F  LeBron James    SF/PF/F/UTIL    9300             52.14
3     UTIL  LeBron James    SF/PF/F/UTIL    9300             52.14

You can add .drop('Roster Position', axis=1) to eliminate that column if you would like.

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