Bird
Raised Fist0
Pandasdata~20 mins

shape for dimensions in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shape Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output of shape attribute on DataFrame
What is the output of the following code snippet?
Pandas
import pandas as pd

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

print(df.shape)
A(9, 1)
B(3,)
C(1, 9)
D(3, 3)
Attempts:
2 left
💡 Hint
The shape attribute returns a tuple with (rows, columns).
❓ data_output
intermediate
1:30remaining
Shape of a Series object
What is the output of the shape attribute for the following pandas Series?
Pandas
import pandas as pd

s = pd.Series([10, 20, 30, 40])
print(s.shape)
A(1, 4)
B4
C(4,)
D(4, 1)
Attempts:
2 left
💡 Hint
Series shape is a tuple with one element representing length.
🔧 Debug
advanced
1:30remaining
Error when accessing shape of a list
What error will this code produce?
Pandas
my_list = [1, 2, 3, 4]
print(my_list.shape)
AAttributeError: 'list' object has no attribute 'shape'
BTypeError: 'list' object is not callable
CNameError: name 'shape' is not defined
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Lists in Python do not have a shape attribute.
🧠 Conceptual
advanced
2:00remaining
Understanding shape for 3D numpy arrays
If a numpy array has shape (4, 3, 2), what does each number represent?
A4 layers, 3 rows, 2 columns
B4 rows, 3 columns, 2 layers
C4 columns, 3 rows, 2 layers
D4 rows, 2 columns, 3 layers
Attempts:
2 left
💡 Hint
Shape is given as (layers, rows, columns) for 3D arrays.
🚀 Application
expert
2:00remaining
Determining shape after DataFrame filtering
Given the DataFrame below, what is the shape after filtering rows where 'Age' > 30?
Pandas
import pandas as pd

data = {'Name': ['Anna', 'Bob', 'Cara', 'Dave'],
        'Age': [25, 35, 45, 28],
        'City': ['NY', 'LA', 'NY', 'SF']}
df = pd.DataFrame(data)

filtered_df = df[df['Age'] > 30]
print(filtered_df.shape)
A(3, 3)
B(2, 3)
C(2, 2)
D(3, 2)
Attempts:
2 left
💡 Hint
Count how many rows have Age greater than 30 and remember columns stay the same.

Practice

(1/5)
1. What does the shape attribute of a pandas DataFrame tell you?
easy
A. The data types of each column
B. The summary statistics of the data
C. The first few rows of the DataFrame
D. The number of rows and columns as a tuple (rows, columns)

Solution

  1. Step 1: Understand what shape returns

    The shape attribute gives the size of the DataFrame as a tuple showing rows and columns.
  2. Step 2: Compare options with this definition

    Only The number of rows and columns as a tuple (rows, columns) correctly describes shape as (rows, columns).
  3. Final Answer:

    The number of rows and columns as a tuple (rows, columns) -> Option D
  4. Quick Check:

    shape = (rows, columns) [OK]
Hint: Remember shape shows (rows, columns) size [OK]
Common Mistakes:
  • Confusing shape with data types
  • Thinking shape shows data preview
  • Mixing shape with summary statistics
2. Which of the following is the correct way to get the shape of a pandas DataFrame named df?
easy
A. df.shape()
B. df.shape
C. df.get_shape()
D. shape(df)

Solution

  1. Step 1: Recall how to access attributes in pandas

    The shape is an attribute, not a method, so no parentheses are used.
  2. Step 2: Check each option

    df.shape uses df.shape correctly without parentheses. Others are incorrect syntax.
  3. Final Answer:

    df.shape -> Option B
  4. Quick Check:

    Use attribute without () for shape [OK]
Hint: Use attribute without parentheses for shape [OK]
Common Mistakes:
  • Adding parentheses like a function call
  • Using non-existent methods like get_shape()
  • Trying to call shape as a standalone function
3. Given the following code:
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
print(df.shape)

What will be the output?
medium
A. (3, 2)
B. (2, 3)
C. [3, 2]
D. Error

Solution

  1. Step 1: Understand the DataFrame structure

    The dictionary has 2 keys (columns) and each key has 3 values (rows).
  2. Step 2: Determine the shape

    So the DataFrame has 3 rows and 2 columns, shape is (3, 2).
  3. Final Answer:

    (3, 2) -> Option A
  4. Quick Check:

    Rows = 3, Columns = 2 [OK]
Hint: Count rows then columns for shape tuple [OK]
Common Mistakes:
  • Swapping rows and columns order
  • Using list brackets instead of tuple
  • Expecting an error from correct code
4. You run this code:
import pandas as pd
df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
print(df.shape())

What is the problem?
medium
A. Column names are invalid
B. DataFrame is empty
C. Using parentheses with shape causes an error
D. Missing import statement

Solution

  1. Step 1: Identify how shape is accessed

    shape is an attribute, not a method, so it should not have parentheses.
  2. Step 2: Understand the error cause

    Calling df.shape() tries to call a tuple as a function, causing a TypeError.
  3. Final Answer:

    Using parentheses with shape causes an error -> Option C
  4. Quick Check:

    Access shape without () to avoid error [OK]
Hint: Don't add () after shape attribute [OK]
Common Mistakes:
  • Treating shape like a function
  • Assuming empty DataFrame causes error
  • Ignoring import errors
5. You have a pandas Series s = pd.Series([10, 20, 30, 40]). What will s.shape return?
hard
A. (4,)
B. (4, 1)
C. 4
D. Error

Solution

  1. Step 1: Understand Series shape

    A pandas Series is one-dimensional, so its shape is a tuple with one element: the number of items.
  2. Step 2: Check the length of the Series

    There are 4 elements, so shape is (4,).
  3. Final Answer:

    (4,) -> Option A
  4. Quick Check:

    Series shape is single-element tuple (length,) [OK]
Hint: Series shape is a one-item tuple with length [OK]
Common Mistakes:
  • Expecting a 2D shape tuple
  • Returning just an integer instead of tuple
  • Thinking shape causes error on Series
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