简体   繁体   English

如果其他两个列在Pandas中具有匹配的值,如何用另一个数据框的值填充空列的值?

[英]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.

相关问题 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe 如何将一列中的值映射到另一个数据帧的另外两列并提取两列上的匹配值? - How to map values in a column to two other column of another dataframe and extract matching values on both columns? 如何通过匹配来自另一个数据帧熊猫的值来填充数据帧中的列的值 - How to fill in values for a column in a dataframe by matching values from another dataframe pandas 熊猫填充列值以具有其他列的相似值 - Pandas fill column values to have similar values of other column(s) 如何用同一列的非缺失值填充熊猫中其他两列的相同值的缺失值? - how to fill missing values with non missing values of same column for identical values of two other columns in pandas? 如何根据另一个数据框中的列填充数据框中的空值? - How to fill empty values in a dataframe based on columns in another dataframe? 检查熊猫数据框中的值是否在另一个数据框中其他两列的任意两个值内 - Check if value in pandas dataframe is within any two values of two other columns in another dataframe Pandas 将 1 列值与另一个数据框列进行比较,找到匹配的行 - Pandas compare 1 columns values to another dataframe column, find matching rows Pandas 将 dataframe 的列重命名为另一个 dataframe 的值,如果两个 Z6A8064B53C47945557755705 列的值匹配 - Pandas rename column of dataframe to value of another dataframe if values of two dataframe columns match Pandas 根据 DataFrame 中的另一列填充 NA 的增量值 - Pandas fill incremental values for NA's according to another column in the DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM