I have a column in pandas data frame like below. Column name is ‘ingredients_text’
Now I want to replace all the values like 5,5% to 5.5% in this column in all the dataframe.
Advertisement
Answer
We can use str.replace
here:
df["ingredients_text"] = df["ingredients_text"].str.replace(r'b(d+),(d+)%', r'1.2%')
The pattern b(d+),(d+)%
matches in the first and second capture groups, respectively, the whole number and decimal component of the percentage. Then we replace with 1.2%
, replacing the comma with dot.