Home > Net >  combine multiple variables in cell to one variable
combine multiple variables in cell to one variable

Time:06-20

I'm trying to change the positions of spellers to a simple format. By changing RW to forward or CM to midfielder. Only there are several values ​​in a cell. How do I combine or drop the other values ​​in the cell?

player player_positions
messi RW, ST, CF
Ronaldo ST,LW

how do i change RW, ST, CF just simple to Forward? Am trying: df.replace(to_replace=r'^RW', value='Forward', regex=True) but then i get:

player player_positions
messi Forward, ST, CF
Ronaldo ST,LW

CodePudding user response:

You can add everything in the replace statement.

df = df.replace(to_replace=r'^RW, ST, CF', value='Forward', regex=True)

or

df = df.replace(to_replace=r'^RW\D*', value='Forward', regex=True)

  • Related