Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas: how to get pandas.compare() to return all columns of the other row?

With dataframes df_a and df_b, how do I return the difference (meaning, the data in other at variance with self) as complete rows (e.g., all columns)? If I do

first = {
    'Name': ['Bob', 'Mike', 'Alex'],
    'Job': ['Forklift Operator', 'Forklift Operator', 'Master Forklift Operator']
}

second = {
    'Name': ['Bob', 'Mike', 'Allen'],
    'Job': ['Forklift Operator', 'Forklift Operator', 'Master Forklift Operator']

df_a = pd.DataFrame(first)
df_b = pd.DataFrame(second)

df_c = df_a.compare(df_b)
print(df_c)

that gives me

    Name
    self  other
2   Alex  Allen

What I would like to be able to get is the entire row from other that does not match the left:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    Name                       Job
2  Allen  Master Forklift Operator

>Solution :

You can use:

df_b.loc[df_a.compare(df_b).index]

Output:

    Name                       Job
2  Allen  Master Forklift Operator
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading