[英]Replace Whole String if it contains substring in pandas dataframe, but with a list of values
Is there a way to do what is done is the below question, but instead of using a single string value, use a dict/array to replace many values in less lines of code?有没有办法做下面的问题,但不是使用单个字符串值,而是使用字典/数组来替换更少代码行中的许多值?
Replace whole string if it contains substring in pandas 如果包含熊猫中的子字符串,则替换整个字符串
What I have so far:到目前为止我所拥有的:
key = [
{
"substr": ["foo1", "foo2"],
"new_val": "bar"
},
]
for i in range(len(key)):
df.loc[df[column].str.contains('|'.join(key[i]['substr'])), column] = key[i]['new_val']
can it be improved?可以改进吗?
Try:尝试:
for el in key:
df[column]=df[column].str.replace('.*('+ '|'.join(el["substr"]) +').*', el["new_val"], regex=True)
Outputs (dummy data):输出(虚拟数据):
import pandas as pd
key = [
{
"substr": ["foo1", "foo2"],
"new_val": "bar"
}
]
df=pd.DataFrame({"x": ["foo1xyz", "abcfoo", "zyc", "foyo2foo2g"], "y": [1,2,3,4]})
for el in key:
df["x"]=df["x"].str.replace('.*('+ '|'.join(el["substr"]) +').*', el["new_val"], regex=True)
>> df
x y
0 bar 1
1 abcfoo 2
2 zyc 3
3 bar 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.