Hint: Use curly braces and colons inside pd.DataFrame() [OK]
Common Mistakes:
Using square brackets instead of parentheses for DataFrame
Using '=' instead of ':' in dictionary
Passing single values instead of lists for column data
3. What will be the output of this code?
import pandas as pd
data = {'Name': ['Anna', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df.shape)
medium
A. (2, 3)
B. (2, 2)
C. (3, 2)
D. (1, 2)
Solution
Step 1: Understand DataFrame shape
DataFrame shape returns (rows, columns).
Step 2: Count rows and columns in data
Data has 2 names and 2 ages, so 2 rows and 2 columns.
Final Answer:
(2, 2) -> Option B
Quick Check:
Rows=2, Columns=2 [OK]
Hint: Shape = (rows, columns) count [OK]
Common Mistakes:
Confusing rows with columns count
Assuming shape returns columns first
Counting keys as rows
4. Identify the error in this DataFrame creation code:
import pandas as pd
data = {'X': [1, 2], 'Y': 3}
df = pd.DataFrame(data)
medium
A. Data values must be lists or arrays, not single integers.
B. Dictionary keys must be integers, not strings.
C. pd.DataFrame() cannot take a dictionary as input.
D. Missing import statement for pandas.
Solution
Step 1: Check data dictionary values
Values are single integers, but DataFrame expects list-like objects for columns.
Step 2: Understand DataFrame input requirements
Each column must have multiple values in a list or array to form rows.
Final Answer:
Data values must be lists or arrays, not single integers. -> Option A
Quick Check:
Values need to be lists [OK]
Hint: Use lists for column data, not single numbers [OK]
Common Mistakes:
Using single values instead of lists
Thinking keys must be integers
Ignoring import pandas statement
5. You have nested data: {'Team': ['A', 'B'], 'Scores': [[10, 20], [15, 25]]}. How do you create a DataFrame that shows each score as a separate row with the team name repeated?
hard
A. Convert nested lists to strings before creating the DataFrame.
B. Create DataFrame directly with nested lists; pandas will flatten automatically.
C. Use pd.DataFrame({'Team': ['A', 'B'], 'Scores': [10, 20, 15, 25]}).
D. Use pd.DataFrame.explode() on the 'Scores' column after creating the DataFrame.
Solution
Step 1: Create DataFrame with nested lists
Creating DataFrame with nested lists keeps 'Scores' as lists in cells.
Step 2: Use explode() to flatten lists into rows
explode() expands each list element into its own row, repeating 'Team' values.
Final Answer:
Use pd.DataFrame.explode() on the 'Scores' column after creating the DataFrame. -> Option D
Quick Check:
explode() flattens nested lists [OK]
Hint: Use explode() to turn list elements into rows [OK]
Common Mistakes:
Expecting pandas to flatten nested lists automatically
Manually flattening data without explode()
Converting lists to strings losing data structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.