Python Pandas - replace based on regex - ignoring rest of the values - regex based vlookup in python

Viewed 65

I got a simple task that I am trying to do but havn't been able to run it successfully so far. I got a dataframe df and a list of regex expressions that I want to check from dataframe df, and corresponding values in my_regex_dict that I want to insert where regex matches.

df = pd.DataFrame ( ['a100','b110','c200','100a'],columns = ["A"])
my_regex_dict = {'.*a.*':'apple', '.*b.*':'ball'}

the data looks as below

df:
    A
0   a100
1   b110
2   c200
3   100a

my_regex_dict:
{'.*a.*': 'apple', '.*b.*': 'ball'}

transforming the dict to make suitable for .replace function in pandas I do following on dict.

my_regex_dict =  {rf'\b{k}\b': v for k, v in my_regex_dict.items()}

and now the dict looks as follow

my_regex_dict:
{'\\b.*a.*\\b': 'apple', '\\b.*b.*\\b': 'ball'}

All I want to do is, check each value in column A, against the dict, and if the regex matches, give me the corresponding value, But if it doesn't, give me blank.

I have done following, which is working, but doing something extra

df["E"] = df.A.str.lower().replace(  dict_regex,regex = True)

and here is what I get as a result

     A       E
0   a100    apple
1   b110    ball
2   c200    c200
3   100a    apple

I want instead is following ie a blank or NULL against c200.

         A       E
0   a100    apple
1   b110    ball
2   c200    
3   100a    apple

Any help would be highly appriciated. Thanks.

1 Answers

You can replace only matched values:

s = df.A.str.lower()
m = s.str.contains('|'.join(my_regex_dict.keys()))
df["E"] = s[m].replace(my_regex_dict,regex = True)
print (df)
      A      E
0  a100  apple
1  b110   ball
2  c200    NaN
3  100a  apple
Related