head(2) returns the first 2 rows of the DataFrame.
Step 2: Identify the first 2 rows in the DataFrame
The first two rows have Names 'Anna' and 'Bob' with Ages 23 and 35 respectively.
Final Answer:
Rows with Name Anna and Bob -> Option C
Quick Check:
head(2) = first 2 rows [OK]
Hint: head(n) shows first n rows [OK]
Common Mistakes:
Thinking head() shows last rows
Assuming head() cannot take arguments
Mixing row order
4. What is wrong with this code snippet?
import pandas as pd
data = {'A': [1, 2, 3, 4, 5]}
df = pd.DataFrame(data)
print(df.tail[3])
medium
A. DataFrame creation is incorrect
B. Using square brackets instead of parentheses with tail
C. tail() cannot be used with numbers
D. Missing import statement
Solution
Step 1: Check the method call syntax
The code uses df.tail[3] which uses square brackets. Methods require parentheses.
Step 2: Correct the syntax
It should be df.tail(3) to call the method with argument 3.
Final Answer:
Using square brackets instead of parentheses with tail -> Option B
Quick Check:
Method calls need parentheses [OK]
Hint: Use parentheses () to call functions [OK]
Common Mistakes:
Using [] instead of () for method calls
Assuming tail() can't take arguments
Ignoring Python syntax errors
5. You have a DataFrame df with 1000 rows. You want to quickly check the first 5 and last 5 rows together. Which code snippet correctly shows this?
hard
A. pd.concat([df.head(), df.tail()])
B. df.head() + df.tail()
C. df.head() & df.tail()
D. df.head().append(df.tail())
Solution
Step 1: Understand how to combine DataFrames
To combine two DataFrames vertically, use pd.concat() or append() method.
Step 2: Check each option
df.head() + df.tail() and df.head() & df.tail() are invalid operations for DataFrames. df.head().append(df.tail()) works but is deprecated in recent pandas versions. pd.concat([df.head(), df.tail()]) is the recommended way.
Final Answer:
pd.concat([df.head(), df.tail()]) -> Option A
Quick Check:
Use pd.concat() to combine DataFrames [OK]
Hint: Use pd.concat() to join DataFrames vertically [OK]
Common Mistakes:
Using + or & operators on DataFrames
Using deprecated append() method
Trying to print head() and tail() separately without combining
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.