I have a string that contains (exclusively) one of several substrings. I want to check which substring is contained and get a value that associated to it. This is why I would do this operation with a dictionary.
Example:
string_to_check = 'TEST13-872B-A22E'
substrings = {'TEST': 0, 'WORLD': 1, 'CORONA':2}
In this case, 0 should be returned.
The background is that I have a pandas DataFrame (df) with a column string_to_check full of these strings. Based on which substring is contained in each row, I want to assign a value to the respective row of a new column of the dataframe.
Example result:
string_to_check result
'TEST13-872B-A22E' 0
'CORONA1-241-22E' 2
'TEST32-33A-442' 0
'WORLD4-BB2-A343' 1
I guess I could use something along the lines of
def check_string(string_to_check):
for stri, val in zip(substrings.keys, substrings.values):
if stri in string_to_check:
return val
combined with apply. But at the moment I feel to stupid to put the pieces together by myself.
EDIT:
Okay I think I solved this myself:
def check_string(string_to_check):
for stri, val in zip(substrings.keys(), substrings.values()):
if stri in string_to_check:
return val
df['result'] = df['string_to_check'].apply(check_string)
But I am happy to see further suggestions for shorter / more readable / more pythonic ways of doing this.