Skip to content Skip to sidebar Skip to footer

How To Get Data In The Right Dataframe That Isn't In The Left Dataframe

I have two data frames and I am trying to output the data that is in one but not the other. I can get the data in the first dataframe but not the second using only_new = old.merge

Solution 1:

It seems you need change '_merge == "left_only"' to '_merge == "right_only"'.

Solution 2:

Consider the dataframes old and new

old = pd.DataFrame(dict(
        ID=[1, 2, 3, 4, 5],
        Type=list('AAABB'),
        Total=[9for _ inrange(5)],
        ArbitraryColumn=['blah'for _ inrange(5)]
    ))

new = pd.DataFrame(dict(
        ID=[3, 4, 5, 6, 7],
        Type=list('ABBCC'),
        Total=[9for _ inrange(5)],
        ArbitraryColumn=['blah'for _ inrange(5)]
    ))

Then to take the symmetrically identical solution

old.merge(
    new, 'outer', on=['ID', 'Type'],
    suffixes=['_', ''], indicator=True  # changed orderof suffixes
).query('_merge == "right_only"').reindex_axis(new.columns, axis=1)
#                   \......../                 \./
#   changed from `left` to `right`      reindex with `new`

  ArbitraryColumn  ID  Total Type
5            blah   69.0    C
6            blah   79.0    C

Post a Comment for "How To Get Data In The Right Dataframe That Isn't In The Left Dataframe"