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)
CodePudding user response:
You could split
/explode
, then replace your values from the list, and agg
back to the original shape:
(df.assign(IDs=df['IDs'].str.split(','), # strings to lists
Names=df['Names'].str.split(' ')
)
.apply(pd.Series.explode) # lists to rows
# map the Names in place of Ids is in my_list
.assign(IDs=lambda d: d['IDs'].mask(d['Names'].isin(my_list), d['Names']))
# reshape back to original by joining
.groupby(level=0).agg({'IDs': ','.join, 'Names': ' '.join})
)
output:
IDs Names
0 APPLE,f,o APPLE ABCD ONE
1 d,f date ABCD
2 d,f,o NO foo YES
3 ORANGE,f ORANGE AVAILABLE
4 d,f TEA AVAILABLE