0
0
Pandasdata~20 mins

head() and tail() for previewing in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Preview Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of head() with n=3?
Given the DataFrame below, what will df.head(3) return?
Pandas
import pandas as pd

data = {'Name': ['Anna', 'Bob', 'Cathy', 'David', 'Eva'], 'Age': [23, 35, 45, 28, 32]}
df = pd.DataFrame(data)
print(df.head(3))
A
   Name  Age
2 Cathy   45
3 David   28
4   Eva   32
B
   Name  Age
3 David   28
4   Eva   32
C
   Name  Age
0  Anna   23
1   Bob   35
2 Cathy   45
3 David   28
4   Eva   32
D
   Name  Age
0  Anna   23
1   Bob   35
2 Cathy   45
Attempts:
2 left
💡 Hint
head(n) shows the first n rows of the DataFrame.
Predict Output
intermediate
2:00remaining
What does tail() return by default?
Given the DataFrame below, what will df.tail() return?
Pandas
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())
A
   City  Population
3 Houston       2.3
4 Phoenix       1.7
B
   City  Population
2 Chicago       2.7
3 Houston       2.3
4 Phoenix       1.7
C
   City  Population
0    NY         8.4
1    LA         4.0
2 Chicago       2.7
3 Houston       2.3
4 Phoenix       1.7
D
   City  Population
0    NY         8.4
1    LA         4.0
Attempts:
2 left
💡 Hint
tail() without argument returns the last 5 rows by default.
data_output
advanced
2:00remaining
How many rows does df.head(0) return?
Consider the DataFrame below. What is the number of rows in df.head(0)?
Pandas
import pandas as pd

data = {'Product': ['A', 'B', 'C', 'D'], 'Price': [10, 20, 30, 40]}
df = pd.DataFrame(data)
print(len(df.head(0)))
A0
B1
C4
DRaises an error
Attempts:
2 left
💡 Hint
head(0) means show zero rows from the top.
Predict Output
advanced
2:00remaining
What is the output of df.tail(2) on a 6-row DataFrame?
Given the DataFrame below, what will df.tail(2) output?
Pandas
import pandas as pd

data = {'Letter': ['a', 'b', 'c', 'd', 'e', 'f'], 'Number': [1, 2, 3, 4, 5, 6]}
df = pd.DataFrame(data)
print(df.tail(2))
A
  Letter  Number
4      e       5
5      f       6
B
  Letter  Number
0      a       1
1      b       2
C
  Letter  Number
3      d       4
4      e       5
D
  Letter  Number
5      f       6
Attempts:
2 left
💡 Hint
tail(2) returns the last two rows.
🧠 Conceptual
expert
2:00remaining
Which statement about head() and tail() is TRUE?
Select the correct statement about head() and tail() methods in pandas.
Ahead(n) returns the last n rows; tail(n) returns the first n rows of a DataFrame.
Bhead(n) returns the first n rows; tail(n) returns the last n rows of a DataFrame.
Chead() and tail() both return the entire DataFrame regardless of n.
Dhead() returns rows randomly; tail() returns rows sorted by index.
Attempts:
2 left
💡 Hint
Think about what 'head' and 'tail' mean in everyday life.