Concat: UNION ALL & UNION

Python · Combining DataFrames (SQL Joins & Unions)

Theory

pd.concat([jan, feb]) stacks DataFrames on top of each other, matching columns by name - the pandas version of UNION ALL. Every row is kept, including repeats, and each frame keeps its own index, so pass ignore_index=True for a fresh 0..n-1 index. keys=['jan', 'feb'] labels where each row came from. Columns missing in one frame are filled with NaN; join='inner' keeps only the columns all frames share.

SQL's UNION (distinct) is concat followed by drop_duplicates(): pd.concat([jan, feb]).drop_duplicates(). With axis=1, concat places frames side by side, aligning rows by index - useful for gluing together columns computed separately, but use merge when rows must match on a key column. DataFrame.append() was removed in pandas 2.0; to add rows, collect DataFrames (or dicts) in a list and call concat once at the end, which is also much faster than adding one row at a time.

Why we use it

Data often arrives in pieces - one file per month, one table per region, new rows to add. concat combines them into one table, and adding drop_duplicates gives a true UNION without repeats.

When to use it

Use concat (axis=0) for UNION ALL of same-shaped tables, add drop_duplicates for UNION, and axis=1 to place aligned columns side by side. Use merge instead whenever rows must be matched on key values.

How it works

  1. pd.concat([jan, feb])
  2. Line up columns by name (missing ones become NaN)
  3. Stack the rows of each frame in list order
  4. Need a fresh 0..n-1 index? Yes: ignore_index=True. No: Keep original labels.
  5. Must repeated rows be removed (UNION)? Yes: .drop_duplicates(). No: Keep all (UNION ALL).
  6. One combined DataFrame

Syntax

pd.concat([df1, df2], ignore_index=True)          # UNION ALL
pd.concat([df1, df2]).drop_duplicates()             # UNION
pd.concat([df1, df2], keys=['a', 'b'])
pd.concat([df_a, df_b], axis=1)                     # side by side

Example

all_rows = pd.concat([jan, feb], ignore_index=True)
distinct = pd.concat([jan, feb]).drop_duplicates(ignore_index=True)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic