'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?
Submitted by Pranit Sharma, on November 11, 2022

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.

Anti-merge columns in Pandas

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.

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 code 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:

Example: 'Anti-merge' in Pandas

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.