简体   繁体   English

替换包含特定子字符串的单元格中的字符

[英]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 .我想找到任何包含子字符串Amount的单元格,并将字符,替换为.

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不知何故面具没有按预期工作

One approach:一种方法:

data["b"] = np.where(data["b"].str.contains("Amount"), data["b"].str.replace(",", "."), data["b"])
print(data)

Output输出

             a            b
0  Amount 3,7$          2,3
1  Amount 6,7$   Amount 3.5
2  Amount 1,3$          4,7

An alternative is to use the following list comprehension:另一种方法是使用以下列表理解:

data["b"] = [x.replace(",", ".") if "Amount" in x else x for x in data["b"]]

If "Amount" is always at the beginning, you could use a more elaborated regex, as below:如果"Amount"总是在开头,您可以使用更详细的正则表达式,如下所示:

data["b"] = data["b"].str.replace("(.*Amount.*)(,)", r"\1.", regex=True)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM