简体   繁体   English

使用字典熊猫填充 NaN 值

[英]Fill NaN values using dictionary pandas

I have a dataframe dfp with columns Brand_ID and Brand_Name (and some more columns like Product_ID , Product_Name etc.)我有一个包含Brand_IDBrand_Name列的数据Brand_ID dfp (还有一些列,如Product_IDProduct_Name等)

Some Brand names are NaN because of multiple brand_IDs separated by comma (see picture)一些品牌名称是 NaN,因为多个品牌 ID 以逗号分隔(见图) 在此处输入图片说明

I want to fill those NaNs with the actual brand names separated by comma.我想用逗号分隔的实际品牌名称填充这些 NaN。 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:对于缺失值的行,使用 lambda 函数分割值,在字典中匹配并连接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM