Python Pandas - Find difference between two data frames

前端 未结 10 1926
陌清茗
陌清茗 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条回答
  •  Happy的楠姐
    2020-11-22 14:43

    For rows, try this, where Name is the joint index column (can be a list for multiple common columns, or specify left_on and right_on):

    m = df1.merge(df2, on='Name', how='outer', suffixes=['', '_'], indicator=True)
    

    The indicator=True setting is useful as it adds a column called _merge, with all changes between df1 and df2, categorized into 3 possible kinds: "left_only", "right_only" or "both".

    For columns, try this:

    set(df1.columns).symmetric_difference(df2.columns)
    

提交回复
热议问题