简体   繁体   English

Python 数据框 = 用来自另一个数据框的值替换列字符串中的值

[英]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规则 ID Rule_Value规则值
R-123 R-123 column1 > param1 and column2 > param2 column1 > param1 和 column2 > param2
R-456 R-456 column1 > param1列 1 > 参数 1
... ... ... ...

There is another Dataframe (RuleMapDF) with the mapping: 还有另一个带有映射的数据框(RuleMapDF):
Rule_ID规则 ID Rule_Param Rule_Param Param_Value参数值
R-123 R-123 param1参数 1 100 100
R-123 R-123 param2参数 2 200 200
R-456 R-456 param1参数 1 100 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规则 ID Rule_Value规则值
R-123 R-123 column1 > 100 and column2 > 200第 1 列 > 100 和第 2 列 > 200
R-456 R-456 column1 > 100第 1 列 > 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)

OUTPUT:输出:

 Rule_ID                       Rule_Value
0   R-123  column1 > 100 and column2 > 200
1   R-456                    column1 > 100
1st Step:第一步:

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}  
2nd Step:第二步:

It'll use the mapping dict to replace the value in the Rule_Value column.它将使用映射字典来替换Rule_Value列中的值。

3rd step:第三步:

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.

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