简体   繁体   中英

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.
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.

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 ie column 0 .

如果您要更新Rule_Value列中的每个字符串

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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