Introduction to Merging/Concatenating DataFrames

This section introduces the concept of merging/concatenating DataFrames in Pandas.

Concept

Till now, you’ve been dealing with a single DataFrame object. Often, there you may need to combine multiple datasets into one object, based on a common column.

Syntax

Assuming you have two DataFrame objects, df1 and df2, the most basic type of merge can be done with the following syntax:

merged = pd.merge(df1, df2)

Multiple options exist in the merge; you could do a Left/Right/Outer join, or choose the columns to perform the merge.

There are also other ways to combine DataFrame objects, such as by using .join() or .append().

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.