Bird
Raised Fist0
Data Analysis Pythondata~20 mins

head() and tail() in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Head and Tail Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of head() with default parameter
What will be the output of the following code snippet?
Data Analysis Python
import pandas as pd

data = {'Name': ['Anna', 'Bob', 'Charlie', 'David', 'Eva'], 'Age': [23, 35, 45, 28, 32]}
df = pd.DataFrame(data)
print(df.head())
A[{'Name': 'Anna', 'Age': 23}, {'Name': 'Bob', 'Age': 35}, {'Name': 'Charlie', 'Age': 45}, {'Name': 'David', 'Age': 28}, {'Name': 'Eva', 'Age': 32}]
B[{'Name': 'Anna', 'Age': 23}, {'Name': 'Bob', 'Age': 35}, {'Name': 'Charlie', 'Age': 45}]
C[{'Name': 'Anna', 'Age': 23}, {'Name': 'Bob', 'Age': 35}, {'Name': 'Charlie', 'Age': 45}, {'Name': 'David', 'Age': 28}]
D[{'Name': 'Anna', 'Age': 23}, {'Name': 'Bob', 'Age': 35}]
Attempts:
2 left
💡 Hint
head() by default shows the first 5 rows of the DataFrame.
❓ Predict Output
intermediate
2:00remaining
Output of tail() with parameter 3
What will be the output of this code?
Data Analysis Python
import pandas as pd

data = {'City': ['NY', 'LA', 'Chicago', 'Houston', 'Phoenix'], 'Population': [8.4, 4.0, 2.7, 2.3, 1.7]}
df = pd.DataFrame(data)
print(df.tail(3))
A[{'City': 'Chicago', 'Population': 2.7}, {'City': 'Houston', 'Population': 2.3}, {'City': 'Phoenix', 'Population': 1.7}]
B[{'City': 'NY', 'Population': 8.4}, {'City': 'LA', 'Population': 4.0}, {'City': 'Chicago', 'Population': 2.7}]
C[{'City': 'Houston', 'Population': 2.3}, {'City': 'Phoenix', 'Population': 1.7}]
D[{'City': 'Phoenix', 'Population': 1.7}]
Attempts:
2 left
💡 Hint
tail(3) shows the last 3 rows of the DataFrame.
❓ data_output
advanced
2:00remaining
Number of rows returned by head() and tail() combined
Given a DataFrame with 10 rows, what is the total number of rows returned when you combine df.head(7) and df.tail(5)?
A7
B10
C12
D5
Attempts:
2 left
💡 Hint
head(7) returns first 7 rows, tail(5) returns last 5 rows. Some rows overlap.
🔧 Debug
advanced
2:00remaining
Identify the error in using head()
What error will this code produce and why? import pandas as pd data = {'A': [1,2,3,4,5]} df = pd.DataFrame(data) print(df.head(-3))
Data Analysis Python
import pandas as pd

data = {'A': [1,2,3,4,5]}
df = pd.DataFrame(data)
print(df.head(-3))
AValueError: negative value not allowed
BSyntaxError
CEmpty DataFrame with 0 rows
DTypeError
Attempts:
2 left
💡 Hint
head() with a negative argument returns an empty DataFrame.
🚀 Application
expert
3:00remaining
Using head() and tail() to sample data
You have a DataFrame with 1000 rows sorted by date ascending. You want to create a new DataFrame that contains the first 10 and last 10 rows combined without duplicates. Which code snippet correctly achieves this?
Apd.concat([df.head(10), df.tail(10)])
Bdf.head(10).append(df.tail(10))
Cdf.head(10) + df.tail(10)
Dpd.concat([df.head(10), df.tail(10)]).drop_duplicates()
Attempts:
2 left
💡 Hint
Use concat and drop_duplicates to avoid repeated rows.

Practice

(1/5)
1. What does the head() function do when used on a DataFrame?
easy
A. Shows the last few rows of the DataFrame
B. Deletes the first few rows of the DataFrame
C. Shows the total number of rows in the DataFrame
D. Shows the first few rows of the DataFrame

Solution

  1. Step 1: Understand the purpose of head()

    The head() function is designed to display the first few rows of a DataFrame to give a quick look at the data.
  2. Step 2: Compare with other options

    tail() shows the last few rows, counting rows is done by len(), and deleting rows is not the function of head().
  3. Final Answer:

    Shows the first few rows of the DataFrame -> Option D
  4. Quick Check:

    head() = first rows [OK]
Hint: Remember: head = start, tail = end [OK]
Common Mistakes:
  • Confusing head() with tail()
  • Thinking head() counts rows
  • Assuming head() deletes data
2. Which of the following is the correct syntax to show the last 3 rows of a DataFrame named df?
easy
A. df.tail(3)
B. df.head(3)
C. df.last(3)
D. df.end(3)

Solution

  1. Step 1: Identify the function for last rows

    The tail() function returns the last rows of a DataFrame, and it accepts a number to specify how many rows.
  2. Step 2: Check syntax correctness

    df.tail(3) is correct; head() shows first rows, and last() or end() are not valid pandas functions.
  3. Final Answer:

    df.tail(3) -> Option A
  4. Quick Check:

    Last rows = tail() [OK]
Hint: Use tail(n) to get last n rows [OK]
Common Mistakes:
  • Using head() instead of tail()
  • Using non-existent functions like last()
  • Wrong method call syntax
3. Given the DataFrame df below:
import pandas as pd
data = {'Name': ['Anna', 'Bob', 'Cara', 'Dan', 'Eva'], 'Age': [23, 35, 45, 29, 40]}
df = pd.DataFrame(data)

What will df.head(2) return?
medium
A. An error because head() needs no arguments
B. Rows with Name Dan and Eva
C. Rows with Name Anna and Bob
D. Rows with Age 45 and 29

Solution

  1. Step 1: Understand what head(2) does

    head(2) returns the first 2 rows of the DataFrame.
  2. 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.
  3. Final Answer:

    Rows with Name Anna and Bob -> Option C
  4. 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

  1. Step 1: Check the method call syntax

    The code uses df.tail[3] which uses square brackets. Methods require parentheses.
  2. Step 2: Correct the syntax

    It should be df.tail(3) to call the method with argument 3.
  3. Final Answer:

    Using square brackets instead of parentheses with tail -> Option B
  4. 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

  1. Step 1: Understand how to combine DataFrames

    To combine two DataFrames vertically, use pd.concat() or append() method.
  2. 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.
  3. Final Answer:

    pd.concat([df.head(), df.tail()]) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard