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
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.

