Bird
Raised Fist0
Pandasdata~10 mins

concat() for stacking DataFrames in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - concat() for stacking DataFrames
Start with DataFrames
↓
Choose axis: 0 for rows, 1 for columns
↓
Call pd.concat() with list of DataFrames
↓
pandas stacks DataFrames along chosen axis
↓
Return new combined DataFrame
This flow shows how pandas concat() stacks DataFrames by choosing an axis and combining them into one.
Execution Sample
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

result = pd.concat([df1, df2], axis=0)
This code stacks two DataFrames vertically (row-wise) using concat with axis=0.
Execution Table
StepActionInput DataFramesAxisResulting DataFrameNotes
1Create df1{'A': [1, 2], 'B': [3, 4]}N/A A B 0 1 3 1 2 4First DataFrame created
2Create df2{'A': [5, 6], 'B': [7, 8]}N/A A B 0 5 7 1 6 8Second DataFrame created
3Call pd.concat([df1, df2], axis=0)[df1, df2]0 (rows) A B 0 1 3 1 2 4 0 5 7 1 6 8DataFrames stacked vertically, index repeated
4Call pd.concat([df1, df2], axis=1)[df1, df2]1 (columns) A B A B 0 1 3 5 7 1 2 4 6 8DataFrames stacked horizontally, columns duplicated
5Call pd.concat([df1, df2], axis=0, ignore_index=True)[df1, df2]0 (rows) A B 0 1 3 1 2 4 2 5 7 3 6 8Index reset to avoid duplicates
6ExitN/AN/AN/ANo more stacking, concat finished
💡 All DataFrames stacked; concat() returns combined DataFrame
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
df1undefined A B 0 1 3 1 2 4 A B 0 1 3 1 2 4 A B 0 1 3 1 2 4 A B 0 1 3 1 2 4 A B 0 1 3 1 2 4 A B 0 1 3 1 2 4
df2undefinedundefined A B 0 5 7 1 6 8 A B 0 5 7 1 6 8 A B 0 5 7 1 6 8 A B 0 5 7 1 6 8 A B 0 5 7 1 6 8
resultundefinedundefinedundefined A B 0 1 3 1 2 4 0 5 7 1 6 8 A B A B 0 1 3 5 7 1 2 4 6 8 A B 0 1 3 1 2 4 2 5 7 3 6 8 A B 0 1 3 1 2 4 2 5 7 3 6 8
Key Moments - 3 Insights
Why do the row indexes repeat after stacking with axis=0?
Because concat() keeps original indexes by default, so rows from both DataFrames keep their own indexes (see execution_table step 3). Use ignore_index=True to reset.
What happens when stacking with axis=1?
DataFrames are joined side-by-side, adding columns. If columns have same names, they appear duplicated (see execution_table step 4).
How does ignore_index=True affect the result?
It resets the row index to a continuous sequence, avoiding duplicate indexes (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3. What is the index of the third row in the resulting DataFrame?
A0
B1
C2
D3
💡 Hint
Check the 'Resulting DataFrame' column at step 3; indexes repeat from original DataFrames.
At which step does concat() stack DataFrames horizontally?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for axis=1 in the 'Axis' column of the execution table.
If we remove ignore_index=True in step 5, what will happen to the index?
AIt will reset to 0,1,2,3
BIt will keep duplicate indexes from original DataFrames
CIt will create a multi-level index
DIt will drop the index column
💡 Hint
Compare step 3 and step 5 results in the execution table to see the effect of ignore_index.
Concept Snapshot
pd.concat([df1, df2], axis=0) stacks DataFrames vertically (rows).
axis=1 stacks horizontally (columns).
By default, indexes are kept; use ignore_index=True to reset.
Result is a new combined DataFrame.
Useful for combining data from multiple sources.
Full Transcript
This visual execution shows how pandas concat() stacks DataFrames. First, two DataFrames df1 and df2 are created. Then, concat() is called with axis=0 to stack rows, resulting in a DataFrame with repeated indexes. Next, concat() with axis=1 stacks columns side-by-side, duplicating column names. Using ignore_index=True resets the row index to avoid duplicates. Variables df1, df2, and result change as concat() runs. Key points include index behavior and axis choice. The quiz tests understanding of index values, stacking direction, and ignore_index effect.

Practice

(1/5)
1. What does the concat() function do in pandas?
easy
A. Deletes rows from a DataFrame
B. Joins DataFrames by stacking rows or columns
C. Sorts the DataFrame by index
D. Filters rows based on a condition

Solution

  1. Step 1: Understand the purpose of concat()

    concat() is used to combine multiple DataFrames either by stacking rows or columns.
  2. Step 2: Compare with other DataFrame operations

    Deleting, sorting, or filtering are different operations and not related to concat().
  3. Final Answer:

    Joins DataFrames by stacking rows or columns -> Option B
  4. Quick Check:

    concat() stacks DataFrames [OK]
Hint: Remember: concat stacks rows or columns [OK]
Common Mistakes:
  • Confusing concat with filtering or sorting
  • Thinking concat deletes data
  • Assuming concat only merges columns
2. Which of the following is the correct syntax to stack two DataFrames df1 and df2 vertically using concat()?
easy
A. pd.concat(df1 + df2)
B. pd.concat([df1, df2], axis=1)
C. pd.concat(df1, df2)
D. pd.concat([df1, df2], axis=0)

Solution

  1. Step 1: Identify axis for vertical stacking

    Vertical stacking means stacking rows, which is done with axis=0.
  2. Step 2: Check syntax correctness

    pd.concat() requires a list of DataFrames as first argument, so [df1, df2] is correct.
  3. Final Answer:

    pd.concat([df1, df2], axis=0) -> Option D
  4. Quick Check:

    Vertical stack uses axis=0 [OK]
Hint: Use axis=0 to stack rows vertically [OK]
Common Mistakes:
  • Using axis=1 for vertical stacking
  • Passing DataFrames without a list
  • Trying to add DataFrames with + inside concat
3. Given the following code:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
result = pd.concat([df1, df2], ignore_index=True)
print(result)

What is the output?
medium
A. DataFrame with 4 rows, original indexes 0,1,0,1
B. DataFrame with 2 rows, columns stacked horizontally
C. DataFrame with 4 rows, index reset from 0 to 3
D. Error because ignore_index is invalid

Solution

  1. Step 1: Understand concat with ignore_index=True

    Concatenating df1 and df2 vertically stacks rows, resulting in 4 rows total. ignore_index=True resets the index to 0,1,2,3.
  2. Step 2: Confirm output structure

    The columns remain the same ('A' and 'B'), rows are stacked, and index is reset.
  3. Final Answer:

    DataFrame with 4 rows, index reset from 0 to 3 -> Option C
  4. Quick Check:

    ignore_index=True resets index [OK]
Hint: ignore_index=True resets row numbers after stacking [OK]
Common Mistakes:
  • Forgetting ignore_index resets index
  • Thinking columns stack horizontally by default
  • Assuming error with ignore_index parameter
4. What is wrong with this code snippet?
import pandas as pd
df1 = pd.DataFrame({'X': [1, 2]})
df2 = pd.DataFrame({'Y': [3, 4]})
result = pd.concat(df1, df2, axis=0)
print(result)
medium
A. pd.concat requires a list of DataFrames as first argument
B. axis=0 stacks columns instead of rows
C. DataFrames must have same columns to concat
D. ignore_index=True is missing

Solution

  1. Step 1: Check pd.concat argument format

    pd.concat() expects a list or iterable of DataFrames as the first argument, but here two DataFrames are passed as separate arguments.
  2. Step 2: Identify correct usage

    The correct call is pd.concat([df1, df2], axis=0) with a list.
  3. Final Answer:

    pd.concat requires a list of DataFrames as first argument -> Option A
  4. Quick Check:

    pd.concat([df1, df2]) needs list [OK]
Hint: Always pass DataFrames as a list to concat [OK]
Common Mistakes:
  • Passing DataFrames as separate arguments
  • Confusing axis parameter meaning
  • Assuming columns must match exactly
5. You have two DataFrames:
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'B': [5, 6], 'C': [7, 8]})

You want to stack them vertically and keep all columns, filling missing values with NaN. Which code will do this correctly?
hard
A. pd.concat([df1, df2], axis=0, ignore_index=True)
B. pd.concat([df1, df2], axis=1)
C. pd.concat([df1, df2], axis=0).fillna(0)
D. pd.concat([df1, df2], axis=0, join='inner')

Solution

  1. Step 1: Understand vertical stacking with different columns

    Stacking vertically with axis=0 combines rows. Since columns differ, missing columns get NaN by default.
  2. Step 2: Check parameters for desired behavior

    ignore_index=True resets index. Default join='outer' keeps all columns and fills missing with NaN.
  3. Step 3: Evaluate other options

    pd.concat([df1, df2], axis=1) stacks columns horizontally, not vertically. pd.concat([df1, df2], axis=0).fillna(0) fills NaN with 0, not requested. pd.concat([df1, df2], axis=0, join='inner') uses join='inner' which keeps only common columns.
  4. Final Answer:

    pd.concat([df1, df2], axis=0, ignore_index=True) -> Option A
  5. Quick Check:

    axis=0 + default join='outer' keeps all columns [OK]
Hint: Use axis=0 and default join to keep all columns [OK]
Common Mistakes:
  • Using axis=1 to stack rows
  • Using join='inner' and losing columns
  • Filling NaN when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard