I have a DataFrame 'df':
id value
0 ABC hi
1 XYZ hey
that I want to compare to a list of strings 'str_list':
str_list = ['abc_123', 'xyz_456']
to find partial matches to then replace the value if the partial match on 'id' is found in the str_list to make something like this:
id value
0 ABC new_value
1 XYZ new_value
As of now I have this code:
df.loc[df['id'].isin(str_list), 'value'] = 'new_val'
but that only works on complete matches (so the df 'id' values would have to be abc_123,xyz_456) in ordre to see the new_vals added.
How can I modify this to accept partial matches?
import pandas as pd
str_list = ['abc_123', 'xyz_456']
df = pd.DataFrame({'id':['ABC','XYZ'], 'value':['hi','hey']})
# this commented out df will trigger the matches correctly
#df = pd.DataFrame({'id':['abc_123','xyz_456'], 'value':['hi','hey']})
print(df)
df.loc[df['id'].isin(str_list), 'value'] = 'new_val'
print(df)