I have a dataframe dfp
with columns Brand_ID
and Brand_Name
(and some more columns like Product_ID
, Product_Name
etc.)
Some Brand names are NaN because of multiple brand_IDs separated by comma (see picture)
I want to fill those NaNs with the actual brand names separated by comma. I have a reference dictionary that I can use for this
For rows with missing values use lambda function for split values, match in dictionary and join:
df = pd.DataFrame({'Brand_ID':['11,12,15','10','15,11'],
'Brand_Name':[np.nan, 'aaa', np.nan]})
x = {'11':'ww', '12':'oup','15':'ret'}
m = df['Brand_Name'].isna()
f = lambda y: ','.join(x[z] for z in y.split(',') if z in x)
df.loc[m, 'Brand_Name'] = df.loc[m, 'Brand_ID'].apply(f)
print (df)
Brand_ID Brand_Name
0 11,12,15 ww,oup,ret
1 10 aaa
2 15,11 ret,ww
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.