0
0
Data Analysis Pythondata~10 mins

concat() for stacking DataFrames in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 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
Combine DataFrames along chosen axis
Return new stacked DataFrame
We start with two or more DataFrames, choose how to stack them (rows or columns), then use concat() to combine them into one DataFrame.
Execution Sample
Data Analysis Python
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.
Execution Table
StepActionDataFrames InvolvedAxisResulting DataFrame ShapeResult Preview
1Create df1df1-(2, 2)[[1,3],[2,4]]
2Create df2df2-(2, 2)[[5,7],[6,8]]
3Call pd.concat([df1, df2], axis=0)df1, df20 (rows)(4, 2)[[1,3],[2,4],[5,7],[6,8]]
4Check index after concatdf1, df20 (rows)(4, 2)Index: [0,1,0,1] (duplicate indices)
5Call pd.concat([df1, df2], axis=0, ignore_index=True)df1, df20 (rows)(4, 2)Index reset to [0,1,2,3]
💡 All DataFrames stacked row-wise; final DataFrame has 4 rows and 2 columns.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5
df1NoneA: [1,2], B: [3,4]A: [1,2], B: [3,4]A: [1,2], B: [3,4]A: [1,2], B: [3,4]
df2NoneNoneA: [5,6], B: [7,8]A: [5,6], B: [7,8]A: [5,6], B: [7,8]
resultNoneNoneNoneA: [1,2,5,6], B: [3,4,7,8], index: [0,1,0,1]A: [1,2,5,6], B: [3,4,7,8], index: [0,1,2,3]
Key Moments - 3 Insights
Why do the indices repeat after stacking without ignore_index?
Because concat keeps original indices by default, so rows from df2 keep their original index values, causing duplicates as shown in step 4 of execution_table.
What does axis=0 mean in concat()?
Axis=0 means stacking DataFrames vertically, adding rows one below another, as shown in step 3 where rows increase from 2 to 4.
How does ignore_index=True affect the result?
It resets the index to a new continuous range starting at 0, avoiding duplicate indices, as shown in step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the shape of the resulting DataFrame?
A(2, 4)
B(2, 2)
C(4, 2)
D(3, 2)
💡 Hint
Check the 'Resulting DataFrame Shape' column at step 3 in execution_table.
At which step does the index get reset to avoid duplicates?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Action' and 'Result Preview' columns in execution_table for index changes.
If axis=1 was used instead of axis=0, how would the shape change after concat?
ARows would double, columns stay same
BColumns would double, rows stay same
CShape stays the same
DDataFrames cannot be concatenated on axis=1
💡 Hint
Recall axis=1 means stacking side-by-side, increasing columns, not rows.
Concept Snapshot
pd.concat([df1, df2], axis=0) stacks DataFrames vertically (rows).
axis=1 stacks horizontally (columns).
By default, indices are kept, causing duplicates.
Use ignore_index=True to reset indices.
Result is a new combined DataFrame.
Full Transcript
We start with two DataFrames, df1 and df2, each with 2 rows and 2 columns. Using pd.concat with axis=0 stacks them vertically, resulting in a DataFrame with 4 rows and 2 columns. By default, the original row indices are kept, so indices repeat. To avoid this, ignore_index=True resets the index to a continuous range. This process creates a new DataFrame combining the data from both inputs.