series dataframe in python

Viewed 34

I am having an data frame which has 4 columns and in which one of the column has data separated by an semicolon. I have to apply a rule for all the values in the column. It is a data frame in python input:

x 
20;30
30;45;64
15;21
18;24
15

I have to make the values 15 and 18 to 21 wherever it occured. so expected output to be:

20;30
30;45;64
21;21
21;24
21

Any leads will be helpful

1 Answers

You can use a regex:

df['x'] = df['x'].str.replace('(?<!\d)(15|18)(?!\d)', '21', regex=True)

output:

          x
0     20;30
1  30;45;64
2     21;21
3     21;24
4        21
Related