Concatenating & Appending
Concatenation in pandas is the operation of gluing DataFrames together along an axis — stacking them vertically to add more rows or side-by-side to add more columns — using pd.concat() , the modern replacement for the removed df.append() method.
Learn Concatenating & Appending in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
The classic use case: you have the same data split across several files (say, one CSV per month) and you want a single combined table. pd.concat([df1, df2]) stacks them on top of each other.
By default it keeps the original index labels, which is why you often pass ignore_index=True for a clean 0..n-1 index.
When you stack vertically, pandas aligns by column name . If one frame has a column the other lacks, the missing cells become NaN . Setting axis=1 instead glues frames side-by-side, aligning by the index .
Often you want to remember which source each row came from. Passing keys=[...] wraps the result in a hierarchical (Multi)Index whose outer level is the label you supplied.
Stack the two frames and give the result a clean 0,1,2,3 index. Fill in the blank argument.
You receive three monthly DataFrames. Combine them into one, tag each row with its month using keys, then total sales per month.
Lesson complete — you can stack data any direction!
You can concatenate frames vertically or horizontally, renumber the index, label each piece with keys, and you know to reach for concat instead of the removed append. Combined with merge, you can assemble data from any number of sources.
🚀 Up next: Pivot Tables & Crosstab — reshape and summarise data in a spreadsheet-style grid.
Practice quiz
What does pd.concat do?
- Matches rows by shared key values
- Sorts a DataFrame
- Stacks DataFrames along an axis
- Removes duplicate columns
Answer: Stacks DataFrames along an axis. concat glues frames together along an axis; it does not match on key values like merge.
What is the default axis for pd.concat?
- axis=0 (stack rows vertically)
- axis=1 (stack columns)
- axis=2
- There is no default
Answer: axis=0 (stack rows vertically). The default axis=0 stacks frames vertically, adding more rows.
What does axis=1 do in pd.concat?
- Stacks rows
- Deletes columns
- Sorts the index
- Glues frames side-by-side, aligning on the index
Answer: Glues frames side-by-side, aligning on the index. axis=1 places frames side-by-side and aligns them by the index.
After pd.concat([jan, feb]) where each frame had index 0,1, what is the result index?
By default the original index labels are kept, giving 0, 1, 0, 1.
What does ignore_index=True produce for that same concat?
ignore_index=True renumbers the result as a clean 0..n-1 RangeIndex.
During a vertical concat, how does pandas align the frames?
- By position only
- By data type
- By column name, filling missing cells with NaN
- It refuses mismatched columns
Answer: By column name, filling missing cells with NaN. Columns align by name; any column missing in one frame becomes NaN there.
What does the keys=['Q1','Q2'] argument add?
- A new data column
- An outer MultiIndex level labelling each source frame
- Sorting by key
- A duplicate check
Answer: An outer MultiIndex level labelling each source frame. keys wraps the result in a hierarchical index whose outer level is the supplied label.
Why was DataFrame.append removed in pandas 2.0?
- It was renamed concat
- It only worked on Series
- It corrupted the index
- It was inefficient, creating a new DataFrame on every call
Answer: It was inefficient, creating a new DataFrame on every call. append rebuilt the whole frame each call; pd.concat once at the end is the efficient replacement.
What is the difference between pd.concat and pd.merge?
- They are the same
- concat stacks similarly-shaped data; merge joins by matching key values
- merge stacks rows; concat joins keys
- concat only works on Series
Answer: concat stacks similarly-shaped data; merge joins by matching key values. Use concat to pile up similar data along an axis; use merge to relate tables by a key.
To total sales per source after concat with keys, what works?
- drop_duplicates()
- sort_values()
- sales
Answer: sales. The outer index level (level=0) holds the key, so grouping by it sums per source.