简体   繁体   English

根据条件使用熊猫追加

[英]Append based on condition using pandas

I have a data frame df1 with columns ID2, Name2, ID3, Name3 . 我有一个数据帧df1 ,其df1 ID2, Name2, ID3, Name3 I have other two data frame df2 (columns: Name2, ID2 ) and df3 (columns: Name3, ID3 ) with the same columns. 我还有其他两个具有相同列的数据框df2 (列: Name2, ID2 )和df3 (列: Name3, ID3 )。 I am trying to merge df2 and df3 based on matching ID, which I do as follows: 我正在尝试根据匹配ID合并df2df3 ,具体操作如下:

Solution = pd.merge(df2,df3, left_on='ID2',right_on='ID3').sort_values('Name2')

Now I want to append this result to df1 , only if ID2/ID3 doesn't already exist in df1 . 现在我想追加这个结果df1 ,只有当ID2 / ID3已经不存在中df1 How do I do that? 我怎么做?

使用isin

df1.append(Solution[~Solution['ID2'].isin(df1['ID1'])])

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

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