0
0
Pandasdata~20 mins

DataFrame as labeled two-dimensional table in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DataFrame Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this DataFrame selection?
Given the DataFrame below, what will be the output of df.loc['b', 'Y']?
Pandas
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20, 30],
    'Y': [40, 50, 60],
    'Z': [70, 80, 90]
}, index=['a', 'b', 'c'])

result = df.loc['b', 'Y']
print(result)
A80
B20
C50
DKeyError
Attempts:
2 left
💡 Hint
Remember that loc selects by label for both rows and columns.
data_output
intermediate
1:30remaining
How many rows and columns does this DataFrame have?
Consider the DataFrame created below. What is the shape (rows, columns) of df?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': range(5),
    'B': range(5, 10),
    'C': range(10, 15)
})

print(df.shape)
A(5, 3)
B(3, 5)
C(5, 5)
D(3, 3)
Attempts:
2 left
💡 Hint
Shape returns (number of rows, number of columns).
🔧 Debug
advanced
1:30remaining
What error does this code raise?
What error will this code produce when run?
Pandas
import pandas as pd

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})

value = df.loc[2, 'col1']
print(value)
ANo error, prints 2
BValueError
CIndexError
DKeyError
Attempts:
2 left
💡 Hint
Check if the row label 2 exists in the DataFrame index.
visualization
advanced
2:00remaining
Which plot shows the correct bar chart of column sums?
Given the DataFrame below, which option shows the correct bar chart of the sum of each column?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

col_sums = df.sum()

plt.bar(col_sums.index, col_sums.values)
plt.show()
ABar chart with bars at A=6, B=15, C=24
BBar chart with bars at A=3, B=5, C=7
CBar chart with bars at A=1, B=4, C=7
DLine chart with points at A=6, B=15, C=24
Attempts:
2 left
💡 Hint
Sum each column and plot those sums as bar heights.
🚀 Application
expert
2:30remaining
What is the value of 'result' after filtering and grouping?
Given the DataFrame below, what is the value of result after filtering rows where 'score' > 70 and grouping by 'team' to get the mean 'score'?
Pandas
import pandas as pd

df = pd.DataFrame({
    'team': ['A', 'B', 'A', 'B', 'C'],
    'score': [65, 85, 75, 90, 60]
})

filtered = df[df['score'] > 70]
result = filtered.groupby('team')['score'].mean()
print(result)
Ateam\nA 75.0\nB 90.0\nName: score, dtype: float64
Bteam\nA 75.0\nB 87.5\nName: score, dtype: float64
Cteam\nA 70.0\nB 87.5\nName: score, dtype: float64
Dteam\nA 70.0\nB 90.0\nName: score, dtype: float64
Attempts:
2 left
💡 Hint
Filter first, then group by team and calculate mean score.