Search code examples
pythonpandasreplacecontainsmask

Replace characters in cells that contain a specific substring


I have the following setup:

data = pd.DataFrame({
    "a": ["Amount 3,7$", "Amount 6,7$", "Amount 1,3$"],
    "b": ["2,3", " Amount 3,5", "4,7"]
})

I want to find any cells with the substring Amount and replace the character , with .

I expect:

    a           b
0   Amount 3.7$     2,3
1   Amount 6.7$     Amount 3.5
2   Amount 1.3$     4,7

I have tried:

for column in data.columns:
    data[column] = data[column][data[column].str.contains("Amount", na=False)].replace(",", ".", regex=True)

What I get is:

    a           b
0   Amount 3.7$     NaN
1   Amount 6.7$     NaN
2   Amount 1.3$     NaN

Somehow the mask is not working as expected


Solution

  • applymap

    data.applymap(lambda s: s.replace(',', '.') if 'Amount' in s else s)
    

                 a            b
    0  Amount 3.7$          2,3
    1  Amount 6.7$   Amount 3.5
    2  Amount 1.3$          4,7