A DataFrame is a two-dimensional data structure where data is aligned in rows and columns. In this answer, we'll learn to find the uncommon rows between the two data frames using pandas.
To find out uncommon rows, we follow the steps below:
We can merge two DataFrames using the concat()
method and remove duplicates using the drop_duplicates()
method.
concat([list of dataframes separated by commas])drop_duplicates()
The concat()
method takes a list of DataFrames as a parameter.
It returns a new DataFrame appending all of them.
The drop_duplicates()
method takes optional parameters like keep
, which decides
import pandas as pd#data frame 1classA = pd.DataFrame({"Student": ['John', 'Lexi', 'Augustin', 'Jane', 'Kate'],"Age": [18, 17, 19, 17, 18]})#data frame 2classB = pd.DataFrame({"Student": ['John', 'Lexi', 'Bob', 'karl', 'Kate'],"Age": [18, 17, 16, 19, 18]})#get uncommon rowsprint(pd.concat([classA,classB]).drop_duplicates(keep=False))
pandas
module, which contains methods to create DataFrames and modify them.class A
, which contains Student
and their Age
.class B
, which contains Student
and their Age
.concat()
method to do so. In this method, we input DataFrames in a list as a parameter to it and remove duplicate rows from the resultant data frame using the drop_duplicates()
method.