This code creates a DataFrame and prints its shape, which shows rows and columns count.
Execution Table
Step
Action
DataFrame State
Shape Value
Output
1
Create DataFrame from dictionary
{'A':[1,2,3], 'B':[4,5,6]}
N/A
DataFrame with 3 rows, 2 columns
2
shape = df.shape
DataFrame with 3 rows, 2 columns
(3, 2)
Tuple (3, 2)
3
Print shape
DataFrame unchanged
(3, 2)
(3, 2) printed to console
💡 Shape attribute accessed and printed; no further steps.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
df
undefined
DataFrame with 3 rows, 2 columns
Same DataFrame
Same DataFrame
shape
undefined
undefined
(3, 2)
(3, 2)
Key Moments - 3 Insights
Why does df.shape return a tuple and not a list or DataFrame?
df.shape always returns a tuple because it is a fixed-size, immutable representation of dimensions (rows, columns). This is shown in execution_table step 2 where the shape value is (3, 2).
What do the two numbers in the shape tuple represent?
The first number is the number of rows, and the second is the number of columns. In the example, (3, 2) means 3 rows and 2 columns, as seen in execution_table step 2.
Can shape change if we modify the DataFrame?
Yes, if you add or remove rows or columns, the shape will update accordingly. Here, shape is checked right after creation (step 2), so it matches the initial size.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the shape of the DataFrame?
A(3, 3)
B(2, 3)
C(3, 2)
D(2, 2)
💡 Hint
Check the 'Shape Value' column in execution_table row for step 2.
According to variable_tracker, what is the value of 'shape' after step 3?
A(3, 2)
B(2, 3)
Cundefined
DDataFrame object
💡 Hint
Look at the 'shape' row under 'After Step 3' in variable_tracker.
If we add a new column to df, how would the shape tuple change?
AThe first number increases by 1
BThe second number increases by 1
CBoth numbers increase by 1
DShape stays the same
💡 Hint
Columns count is the second number in the shape tuple, as explained in key_moments.
Concept Snapshot
Use .shape on pandas DataFrame or array to get dimensions.
Returns a tuple: (rows, columns).
Rows count is first, columns second.
Useful for checking data size quickly.
Shape updates if data changes.
Full Transcript
This visual execution shows how to get the shape of a pandas DataFrame. First, a DataFrame is created from a dictionary with 3 rows and 2 columns. Then, accessing the .shape attribute returns a tuple (3, 2) representing rows and columns. Printing this tuple shows the size of the DataFrame. The shape is immutable and updates if the DataFrame changes. This helps understand the data's dimensions quickly.