Replace function only works on strings and not substrings

Viewed 25

I have a dataframe with multiple columns and rows. I'm trying to replace commas with dots in all strings in the dataframe, e.g. I want to change value 'John, Doe' to 'John. Doe'

I'm using the following line:

df_fulfill = df_fulfill.replace(',','.')

However, this line only replaces values that exactly match the 1st argument. Hence, it only replaces strings that are equal to ',', whereas strings that contain like 'John, Doe' are ignored.

This is the full code I'm using:

import pandas as pd

fulfill_workbook = 'fulfill 7.csv'

df_fulfill = pd.read_csv(fulfill_workbook, sep=';', encoding='latin-1')

df_fulfill = df_fulfill.astype(str).replace(',','.')

df_fulfill.to_excel('fulfill ready.xlsx')

I'd really appreciate any help. Thanks

1 Answers

I was missing regex=True in replace. The following line worked:

df_fulfill = df_fulfill.replace(',','.', regex=True)
Related