I have a dataframe and a list as follows:
import pandas as pd
import numpy as np
df = pd.DataFrame({'IDs':['d,f,o','d,f','d,f,o','d,f','d,f'],
'Names':['APPLE ABCD ONE','date ABCD','NO foo YES','ORANGE AVAILABLE','TEA AVAILABLE']})
my_list = ['APPLE', 'ORANGE', 'LEMONS', 'STRAWBERRY', 'BLUEBERRY']
I would like to replace the comma separated values in the IDs column with the corresponding values from the Names column in case they appear in my_list.
desired output:
df.IDs => ['APPLE,f,o', 'd,f', 'd,f,o', 'ORANGE,f', 'd,f']
to find out whether the row contains the values in the list I have tried:
df['Names'].apply(lambda x: any([k in x for k in my_list]))
and to replace the values in the IDs column I have tried the following but I am not sure how to indicate that only the corresponding value should change,
df.IDs.apply(lambda i: i if i in my_list else 'don't know what to do here')
and i think I can use np.where() to perform the whole replacement based on conditions
np.where(df['Names'].apply(lambda x: any([k in x for k in my_list])) == True, df.IDs.apply(lambda i: i if i in my_list else 'don't know what to do here'), df.IDs)