0
0
Pandasdata~20 mins

shape for dimensions in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 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.