Python dataframe = Replace values in column string with values from another dataframe

Viewed 88

I have the following dataframe (RuleDF) with a column which contains a string.
Inside the string are some values (parameters) which shall be replaced
eg. param1 and param2

Rule_ID Rule_Value
R-123 column1 > param1 and column2 > param2
R-456 column1 > param1
... ...

There is another Dataframe (RuleMapDF) with the mapping:
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.

3 Answers

As @Nk03, first create a mapping dict for each Rule_ID from df2 to allow string substitution with replace() method:

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. We can left merge the result with the original df1.

  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.

3rd step:

Drop the map_dict column i.e column 0.

If you're updating every string in the Rule_Value column

dataframe.at[index,'Rule_Value']='new value'
Related