简体   繁体   English

Python/Pandas:仅在 string.startswith 特定字符串时应用替换

[英]Python/Pandas: Apply replacement only when string.startswith a specific string

Using pandas to read an excel file.使用 pandas 读取 excel 文件。 The following code is working fine for one column:以下代码适用于一列:

data["ProtocolTCP"] = data["ProtocolTCP"].str.replace("tcp-", "tcp ")

However, I discovered some cells have the value in the middle of the string and do not want to change those.但是,我发现某些单元格的值位于字符串中间,并且不想更改这些值。

How can I just apply the same command to just those string that startswith "tcp-" only?如何仅将相同的命令应用于仅以“tcp-”开头的字符串?

I think a "lambda" command would work but have a hard time figuring it out.我认为“lambda”命令会起作用,但很难弄清楚。 Or perhaps there is a better option than lambda?或者也许有比 lambda 更好的选择?

You can add ^你可以加^

data["ProtocolTCP"] = data["ProtocolTCP"].str.replace("^tcp-", "tcp ")

You can use np.where() which performs better than a lambda I think:您可以使用性能比 lambda 更好的np.where()我认为:

data["ProtocolTCP"] = np.where(data["ProtocolTCP"].startswith("tcp-"),data["ProtocolTCP"].str.replace("tcp-","tcp "),data["ProtocolTCP"])

Another option using list comprehension:使用列表理解的另一个选项:

data["ProtocolTCP"] = [x.replace("tcp-","tcp ") if x.startswith("tcp-") for x in data["ProtocolTCP"].values]

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

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