Home »
Python »
Python Programs
'Anti-merge' in Pandas
Learn, how to anti merge columns pandas dataframe, by anti-merge we mean picking out the different values from the columns?
By Pranit Sharma Last updated : October 05, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose, we are given two dataframes A and B with a common column X. We need to pick out the differences between the columns of the same name in two dataframes.
Whenever we apply() merge method on our dataframes with respect to some column, we always get the common values of the column present in both the dataframes but we need to identify the other values as well.
Anti-merge columns in Pandas
For this purpose, we need to change the merge type to 'outer' and indicator = True which will add a column that represents whether the values are from the left dataframe of right dataframe, or both.
Pandas merge() is a method of combining or joining two DataFrames but the key point is merge method allows us to combine the DataFrames based on specific columns instead of index values.
Let us understand with the help of an example,
Python program for anti-merge columns in Pandas
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating two dictionaries
d1 = {'x':np.arange(5)}
d2 = {'x':np.arange(3.8)}
# Creating DataFrames
A = pd.DataFrame(d1)
B = pd.DataFrame(d2)
# Display dataframes
print('Original DataFrame 1:\n',A,'\n')
print('Original DataFrame 2:\n',B,'\n')
# merging dataframes and checking all columns
res = pd.merge(A,B, how='outer', indicator=True)
# Display result
print('Result:\n',res,'\n')
Output
The output of the above program is:
Python Pandas Programs »