Python Pandas - Find difference between two data frames

前端 未结 10 1838
陌清茗
陌清茗 2020-11-22 13:59

I have two data frames df1 and df2, where df2 is a subset of df1. How do I get a new data frame (df3) which is the difference between the two data frames?

In other w

10条回答
  •  长发绾君心
    2020-11-22 14:45

    As mentioned here that

    df1[~df1.apply(tuple,1).isin(df2.apply(tuple,1))]
    

    is correct solution but it will produce wrong output if

    df1=pd.DataFrame({'A':[1],'B':[2]})
    df2=pd.DataFrame({'A':[1,2,3,3],'B':[2,3,4,4]})
    

    In that case above solution will give Empty DataFrame, instead you should use concat method after removing duplicates from each datframe.

    Use concate with drop_duplicates

    df1=df1.drop_duplicates(keep="first") 
    df2=df2.drop_duplicates(keep="first") 
    pd.concat([df1,df2]).drop_duplicates(keep=False)
    

提交回复
热议问题