[英]Python dataframe = Replace values in column string with values from another dataframe
I have the following dataframe (RuleDF) with a column which contains a string.我有以下数据框(RuleDF),其中有一列包含字符串。
Inside the string are some values (parameters) which shall be replaced字符串内部是一些应替换的值(参数)
eg.例如。 param1 and param2
参数 1 和参数 2
Rule_ID![]() |
Rule_Value![]() |
---|---|
R-123 ![]() |
column1 > param1 and column2 > param2 ![]() |
R-456 ![]() |
column1 > param1![]() |
... ![]() |
... ![]() |
Rule_ID![]() |
Rule_Param ![]() |
Param_Value![]() |
---|---|---|
R-123 ![]() |
param1![]() |
100 ![]() |
R-123 ![]() |
param2![]() |
200 ![]() |
R-456 ![]() |
param1![]() |
100 ![]() |
The result of the replacement will be something like this:替换的结果将是这样的:
Another option is to have a new column with the replaced string另一种选择是有一个带有替换字符串的新列
Rule_ID![]() |
Rule_Value![]() |
---|---|
R-123 ![]() |
column1 > 100 and column2 > 200![]() |
R-456 ![]() |
column1 > 100![]() |
... ![]() |
... ![]() |
I appreciate any ideas.我很欣赏任何想法。 Thank you.
谢谢你。
As @Nk03, first create a mapping dict for each Rule_ID from df2 to allow string substitution with replace()
method:作为@Nk03,首先为df2 中的每个Rule_ID 创建一个映射字典,以允许使用
replace()
方法进行字符串替换:
params = df2.groupby('Rule_ID') \
.apply(lambda x: dict(zip(x['Rule_Param'], x['Param_Value'].astype(str)))) \
.to_dict()
out = df1.groupby('Rule_ID') \
.apply(lambda x: x['Rule_Value'].replace(params[x.name], regex=True))
>>> params
{'R-123': {'param1': '100', 'param2': '200'}, 'R-456': {'param1': '100'}}
>>> out
Rule_ID
R-123 0 column1 > 100 and column2 > 200
R-456 1 column1 > 100
Name: Rule_Value, dtype: object
One Way:单程:
df3 = df1.merge(df2.groupby('Rule_ID').apply(lambda x: dict(
x[['Rule_Param', 'Param_Value']].values)).reset_index(), on='Rule_ID', how='left')
df3['Rule_Value'] = df3.apply(lambda x: ' '.join(
str(x[0].get(i, i)) for i in x['Rule_Value'].split()), 1)
df3 = df3.drop(0, 1)
Rule_ID Rule_Value
0 R-123 column1 > 100 and column2 > 200
1 R-456 column1 > 100
It will create a mapping dict for each Rule_ID
from df2.它将为来自 df2 的每个
Rule_ID
创建一个映射字典。 We can left merge
the result with the original df1
.我们可以
left merge
结果与原始df1
进行left merge
。
Rule_ID Rule_Value \
0 R-123 column1 > param1 and column2 > param2
1 R-456 column1 > param1
0
0 {'param1': 100, 'param2': 200}
1 {'param1': 100}
It'll use the mapping dict to replace the value in the Rule_Value
column.它将使用映射字典来替换
Rule_Value
列中的值。
Drop the map_dict
column ie column 0
.删除
map_dict
列,即第0
列。
如果您要更新Rule_Value
列中的每个字符串
dataframe.at[index,'Rule_Value']='new value'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.