[英]How to fill empty column values with another dataframe's value if two other columns have matching values in Pandas?
I have two similar dataframes. 我有两个类似的数据框。 I want to populate df2['Material']
with the value from df1['Material']
if df1['PartNumber']
and df2['PartNumber']
match. 如果df1['PartNumber']
和df2['PartNumber']
匹配,我想用df1['Material']
的值填充df2['Material']
。 Do can I accomplish this with Pandas (or Python in general)? 我可以用Pandas(或一般来说是Python)完成此操作吗? The data frames are several thousand lines each, these are just snippets. 数据帧各有几千行,它们仅仅是片段。
df1
PartNumber Material ProgramNo Machine
114 JEFD0302000 E 304L O0219 CHNC III
218 REFD0502050 B 6AL-4V O0295 CHNC III
df2
PartNumber ProgramNo Machine Material
0 JEFD0302670 A 6109 + 6609 WY-100 NaN
1 JEFD0510820 A 6110 + 6610 WY-100 NaN
你可以做 :
df2['Material']=df2['PartNumber'].map(dict(zip(df1['PartNumber'],df1['Material']))).fillna(df2.Material)
Using np.where
with map
在map
使用np.where
s=df2.PartNumber.map(df1.set_index('PartNumber').Material)
df2.Material=np.where(df2.PartNumber.isnull(),s,df2.Material)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.