Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching pandas substring with dict key and replacing with dict value

Assume I have df and d below. I want to, for each row in col, check if there is a match with an item key in d, and if so replace the value in col with the corresponding item value, otherwise drop that row. (There can't be more than one match).

df = pd.DataFrame({'col': ['sdffzdhellojkh', 'fegky', 'ouewfzdworldqf']})

d = {'fzdhello': 'hello', 'fzdworld': 'world'}

The output in this case would look like:

df
    col
0   hello
1   world
like image 952
CHRD Avatar asked Jan 24 '23 19:01

CHRD


1 Answers

series.str.extract then map:

df['col'].str.extract('('+ '|'.join(d.keys()) + ')',expand=False).map(d).dropna()

0    hello
2    world
Name: col, dtype: object
like image 123
anky Avatar answered Feb 02 '23 01:02

anky