AI, machine learning and deep learning · Introductory

Concatenating DataFrames

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

Concatenation combines multiple DataFrames into a single object using the pandas concat function, which is the standard method for merging datasets. The operation direction is controlled by the axis parameter, where axis=0 stacks rows vertically and axis=1 places columns horizontally.

Intuition

Think of concatenation as gluing separate data tables together to form a complete picture. If you have two datasets with the same structure, like red and white wine quality data, you can stack them on top of each other to get a comprehensive view. Alternatively, if you have distinct features, you can place them side-by-side. This process is fundamental in data wrangling when information is spread across multiple files, allowing you to unify them for analysis or modeling.

Key equations

winetotal=pd.concat([data1,data2],axis=0)
(1)

Combines data1 and data2 vertically (row-wise) into a new DataFrame named wine_total

pd.concat([dataframea,dataframeb],axis=1)
(2)

Combines dataframe_a and dataframe_b horizontally (column-wise) side-by-side

Derivation

  1. Identify the need to merge separate datasets for analysis.
  2. Select the pd.concat function as the primary tool for combining DataFrames.
  3. Determine the direction of combination: use axis=0 for vertical stacking (rows) or axis=1 for horizontal placement (columns).
  4. Execute the concatenation, noting that axis=0 sums the row counts and axis=1 aligns by index.
  5. Verify the result by checking the shape of the new DataFrame.

Worked example

Problem

You have two DataFrames: data1 with 1599 rows and data2 with 4898 rows. You need to combine them vertically to create a complete dataset. What is the resulting row count?

  1. Identify the operation as vertical concatenation using axis=0.
  2. Sum the row counts of the input DataFrames: 1599 + 4898.
  3. Calculate the total: 6497 rows.

Answer The resulting DataFrame has 6497 rows.

Common mistakes

  • Confusing axis=0 with horizontal concatenation; axis=0 actually stacks rows vertically.
  • Confusing axis=1 with vertical concatenation; axis=1 actually places DataFrames side-by-side horizontally.
  • Assuming append is the primary method for combining DataFrames; concat is the standard for combining multiple DataFrames, while append is for adding single rows.
  • Ignoring index alignment when using axis=1, which can lead to misaligned data or unexpected NaN values if indices do not match.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.