Compare Two pandas DataFrames in Python (Example) | Find Differences Row by Row | merge() Function

Published: 20 July 2022
on channel: Statistics Globe
13,582
167

How to find different rows between two pandas DataFrames in the Python programming language. More details: https://statisticsglobe.com/compare-t...
Python code of this video:

import pandas as pd # Import pandas library in Python

data1 = pd.DataFrame({'x1':range(20, 26), # Create first pandas DataFrame
'x2':['a', 'a', 'a', 'b', 'b', 'b'],
'x3':range(21, 27)})
print(data1) # Print first pandas DataFrame

data2 = pd.DataFrame({'x1':range(22, 29), # Create second pandas DataFrame
'x2':['a', 'a', 'b', 'b', 'b', 'b', 'b'],
'x3':range(23, 30)})
print(data2) # Print second pandas DataFrame

data_12 = data1.merge(data2, # Merge DataFrames with indicator
indicator = True,
how = 'outer')
print(data_12) # Print merged DataFrames

data_12_diff = data_12.loc[lambda x : x['_merge'] != 'both'] # Remove shared rows
print(data_12_diff) # Print differences between DataFrames

Follow me on Social Media:
Facebook – Statistics Globe Page:   / statisticsglobecom  
Facebook – R Programming Group for Discussions & Questions:   / statisticsglobe  
Facebook – Python Programming Group for Discussions & Questions:   / statisticsglobepython  
LinkedIn – Statistics Globe Page:   / statisticsglobe  
LinkedIn – R Programming Group for Discussions & Questions:   / 12555223  
LinkedIn – Python Programming Group for Discussions & Questions:   / 12673534  
Twitter:   / joachimschork  

Music by bensound.com


On this page of the site you can watch the video online Compare Two pandas DataFrames in Python (Example) | Find Differences Row by Row | merge() Function with a duration of hours minute second in good quality, which was uploaded by the user Statistics Globe 20 July 2022, share the link with friends and acquaintances, this video has already been watched 13,582 times on youtube and it was liked by 167 viewers. Enjoy your viewing!