A DataFrame is a two-dimensional data structure in which data is aligned in rows and columns. In this Answer, we'll learn how to find the common rows between two DataFrames using the `merge()` function in Python pandas.
df1.merge(df2, how = 'inner' ,indicator=False)
In the syntax above, df1
and df2
are two DataFrames.
We pass inner
as a value to the how
parameter to get the common rows between two DataFrames. This operation is similar to InnerJoin
in SQL.
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', 'Jane'],"Age": [18, 17, 16, 19, 18, 20]})#get uncommon rowsprint(classA.merge(classB, how = 'inner' ,indicator=False))
In the above code snippet,
pandas
module, which contains methods to create DataFrames and modify them.classA
and classB
by merging both DataFrames using the merge()
method and passing inner
as the value to the how
parameter.Free Resources