Skip to main content

Pandas DataFrame Union Operation

In Pandas DataFrame Set Operation we can use concat() method to perform union operation. concat() function does a union of two or more dataframe. Union function in pandas is similar to the union but here we can remove all the data which are duplicates.

Union in pandas is carried out in two steps:

  • The first step is using concat() which can do concatenation operation.
  • The second step is removing all the duplicate data from the dataframe.

Example

We will create two dataframe.

import pandas as pd

A= pd.DataFrame ({"name":["Jack","Alice"],
"Roll_No":[13,15]})
print(A)# first dataframe

Output: 

    name  Roll_No
0   Jack       13
1  Alice       15
B= pd.DataFrame ({"name":["Jack","Monica"],
"Roll_No":[13,19]})
print(B)#second dataframe

Output: 

     name  Roll_No
0    Jack       13
1  Monica       19
all_values=pd.concat([A,B], ignore_index = True)#concatention operation.
print(all_values)

Output:

     name  Roll_No
0    Jack       13
1   Alice       15
2    Jack       13
3  Monica       19
all_values = all_values.drop_duplicates()#remove duplicate values
print(all_values)

Output:

     name  Roll_No
0    Jack       13
1   Alice       15
3  Monica       19
Submitted by devanshi.srivastava on February 15, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.