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
Mon, 02/15/2021 - 18:39

Authored by

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