concat
pd.concat([df1, df2]) stacks DataFrames on top of each other, aligning columns by name; axis=1 places them side by side, aligning on the index. Pass ignore_index=True to renumber the rows, since the original index labels are kept by default.
Syntax
pd.concat(objs, axis=0, join="outer", ignore_index=False, keys=None)| Part | What it is |
|---|---|
objs | A list of DataFrames or Series |
axis | 0 stacks rows (default), 1 adds columns |
join | "outer" keeps all columns (NaN where missing), "inner" keeps only shared ones |
ignore_index | True gives the result a fresh 0..n-1 index |
keys | Labels that become an outer index level identifying each source |
Examples
import pandas as pd
jan = pd.DataFrame({"item": ["A", "B"], "qty": [5, 3]})
feb = pd.DataFrame({"item": ["A", "C"], "qty": [2, 7]})
pd.concat([jan, feb], ignore_index=True)Result: item qty 0 A 5 1 B 3 2 A 2 3 C 7
pd.concat([jan, feb], keys=["jan", "feb"])Result: item qty jan 0 A 5 1 B 3 feb 0 A 2 1 C 7
keys records which frame each row came from as an index level.
left = pd.DataFrame({"a": [1, 2]})
right = pd.DataFrame({"b": [3, 4]})
pd.concat([left, right], axis=1)Result: a b 0 1 3 1 2 4
Common mistakes
- Duplicate index labels after concat (0, 1, 0, 1) break loc and later joins: pass ignore_index=True.
- Unexpected NaN columns: the frames had different column names (for example Qty vs qty); rename before concatenating.
- Appending in a loop with concat is slow: collect the frames in a list and call pd.concat once at the end.
Practice concat
These drills check your answer on live data, free, no account needed.
- Stack two tables with concatStack `jan` on top of `feb` into one table with a fresh 0, 1, 2, ... index.
Questions
How do I add a single row to a DataFrame?
Build a one-row DataFrame and concat: pd.concat([df, pd.DataFrame([row_dict])], ignore_index=True). For many rows, collect them first and concat once.
What is the difference between concat and merge?
concat stacks frames by position or index without matching keys; merge joins on key values like SQL. Use concat for the same columns from several files, merge to combine related tables.
How do I concatenate many CSV files?
frames = [pd.read_csv(f) for f in files]; df = pd.concat(frames, ignore_index=True).
Related
- mergeHow to merge DataFrames in pandas like a SQL join: on a shared column, on different key names, left and outer joins, suffixes for clashing columns, validate.
- pd.read_csvHow to read a CSV file in pandas with pd.read_csv: separators, headers, index columns, parsing dates, selecting columns, dtypes and the errors people hit.
- set_index and reset_indexHow to use set_index and reset_index in pandas: make a column the row index, go back to a plain column, drop the index, and flatten a groupby result.
- drop_duplicatesHow to remove duplicates in pandas with drop_duplicates: whole rows or a subset of columns, keep first, last or none, and finding duplicates with duplicated.