Bird
Raised Fist0
Pandasdata~20 mins

Box plots in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Box Plot Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple box plot data summary
What is the output of the following code snippet that creates a box plot summary using pandas?
Pandas
import pandas as pd
import numpy as np

data = pd.DataFrame({'values': np.array([1, 2, 2, 3, 4, 5, 6, 7, 8, 9])})
summary = data['values'].describe()
summary
A{'count': 10.0, 'mean': 4.7, 'std': 2.738613, 'min': 1.0, '25%': 2.5, '50%': 4.5, '75%': 7.0, 'max': 9.0}
B{'count': 10.0, 'mean': 5.0, 'std': 3.0, 'min': 1.0, '25%': 3.0, '50%': 5.0, '75%': 7.0, 'max': 9.0}
C{'count': 10.0, 'mean': 4.7, 'std': 2.738613, 'min': 1.0, '25%': 2.75, '50%': 4.5, '75%': 7.0, 'max': 9.0}
D{'count': 10.0, 'mean': 4.7, 'std': 2.738613, 'min': 1.0, '25%': 2.75, '50%': 5.0, '75%': 7.0, 'max': 9.0}
Attempts:
2 left
💡 Hint
Look carefully at the quartile values and mean calculated by pandas describe() method.
❓ data_output
intermediate
2:00remaining
Number of outliers detected in a box plot
Given the following data, how many outliers will be detected using the IQR method in a box plot?
Pandas
import pandas as pd
import numpy as np

data = pd.Series([10, 12, 12, 13, 12, 11, 14, 15, 100, 101, 102])
Q1 = data.quantile(0.25)
Q3 = data.quantile(0.75)
IQR = Q3 - Q1
outliers = data[(data < (Q1 - 1.5 * IQR)) | (data > (Q3 + 1.5 * IQR))]
outliers.count()
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Calculate Q1, Q3, then find values outside 1.5 times IQR range.
❓ visualization
advanced
2:30remaining
Interpreting box plot visualization with multiple categories
You have this code to create box plots for two groups. Which statement correctly describes the visualization output?
Pandas
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
data = pd.DataFrame({'group': ['A']*50 + ['B']*50, 'value': np.concatenate([np.random.normal(0, 1, 50), np.random.normal(1, 1.5, 50)])})
data.boxplot(by='group', column='value')
plt.show()
AGroup B has a higher median and wider spread than group A.
BGroup A has a higher median and wider spread than group B.
CBoth groups have the same median but different spreads.
DGroup B has a lower median and narrower spread than group A.
Attempts:
2 left
💡 Hint
Look at the mean and standard deviation used to generate each group.
🧠 Conceptual
advanced
1:30remaining
Understanding whiskers in box plots
In a box plot created by pandas, what do the whiskers represent by default?
AThe minimum and maximum values in the data.
BThe most extreme data points within 1.5 times the IQR from the quartiles.
CThe 1st and 3rd quartiles.
DThe mean plus or minus one standard deviation.
Attempts:
2 left
💡 Hint
Think about how box plots identify outliers.
🔧 Debug
expert
2:00remaining
Identify the error in box plot code with missing data
What error will this code raise when trying to plot a box plot with missing values?
Pandas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data = pd.Series([1, 2, np.nan, 4, 5])
data.plot.box()
plt.show()
AAttributeError because plot.box() is not a valid method.
BValueError due to NaN values in the data.
CTypeError because NaN is not a number.
DNo error; the box plot will ignore NaN values and plot correctly.
Attempts:
2 left
💡 Hint
Check how pandas handles NaN values in plotting functions.

Practice

(1/5)
1. What does a box plot primarily show about a dataset?
easy
A. Only the average value of the data
B. The spread, median, and outliers of the data
C. The exact values of all data points
D. The correlation between two variables

Solution

  1. Step 1: Understand box plot components

    A box plot shows the median line, the box representing the middle 50% of data, and whiskers for spread.
  2. Step 2: Identify what it reveals

    It highlights the data spread, median, and any outliers beyond whiskers.
  3. Final Answer:

    The spread, median, and outliers of the data -> Option B
  4. Quick Check:

    Box plot = spread + median + outliers [OK]
Hint: Box plots show spread and outliers, not just averages [OK]
Common Mistakes:
  • Thinking box plots show all data points exactly
  • Confusing box plots with scatter plots
  • Assuming box plots show correlation
2. Which of the following is the correct way to create a box plot for a pandas DataFrame named df?
easy
A. boxplot(df)
B. df.plot.boxplot()
C. df.boxplot()
D. df.box()

Solution

  1. Step 1: Recall pandas box plot syntax

    The pandas DataFrame method to create a box plot is boxplot().
  2. Step 2: Check each option

    df.boxplot() uses df.boxplot(), which is correct. df.plot.boxplot() is invalid syntax. boxplot(df) is not a pandas function. df.box() is invalid.
  3. Final Answer:

    df.boxplot() -> Option C
  4. Quick Check:

    Use df.boxplot() to plot box plots [OK]
Hint: Use df.boxplot() to create box plots in pandas [OK]
Common Mistakes:
  • Using df.plot.boxplot() which is invalid
  • Trying to call boxplot as a standalone function
  • Using df.box() which does not exist
3. Given the following code, what will be the output type of ax?
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'A': [1, 2, 5, 7, 9],
    'B': [2, 3, 5, 8, 10]
})
ax = df.boxplot()
medium
A. A matplotlib AxesSubplot object
B. A list of numbers
C. A pandas DataFrame
D. A matplotlib Figure object

Solution

  1. Step 1: Understand df.boxplot() return type

    The boxplot() method returns a matplotlib AxesSubplot object representing the plot area.
  2. Step 2: Check options against expected type

    The return type is a matplotlib AxesSubplot object. This is not a pandas DataFrame, not a list of numbers, and not a matplotlib Figure object.
  3. Final Answer:

    A matplotlib AxesSubplot object -> Option A
  4. Quick Check:

    df.boxplot() returns AxesSubplot [OK]
Hint: df.boxplot() returns a matplotlib Axes object [OK]
Common Mistakes:
  • Expecting a DataFrame or list as output
  • Confusing Figure and Axes objects
  • Assuming no return value
4. What is wrong with this code snippet that tries to create a box plot grouped by a column?
import pandas as pd
df = pd.DataFrame({
    'Category': ['X', 'X', 'Y', 'Y'],
    'Value': [10, 15, 10, 20]
})
df.boxplot(column='Value', by='Category', groupby='Category')
medium
A. The argument 'groupby' is invalid for boxplot()
B. The 'by' argument should be 'groupby'
C. The 'column' argument should be a list, not a string
D. The DataFrame is missing numeric columns

Solution

  1. Step 1: Check boxplot() arguments

    The pandas boxplot method accepts 'column' and 'by' arguments, but not 'groupby'.
  2. Step 2: Identify invalid argument

    The code incorrectly uses 'groupby' which causes an error. 'by' is correct for grouping.
  3. Final Answer:

    The argument 'groupby' is invalid for boxplot() -> Option A
  4. Quick Check:

    Use 'by' to group, no 'groupby' argument [OK]
Hint: Use 'by' to group in boxplot(), not 'groupby' [OK]
Common Mistakes:
  • Using 'groupby' instead of 'by' argument
  • Passing column as list when string is allowed
  • Thinking numeric columns are missing
5. You have a DataFrame df with columns "Group" and "Score". You want to compare the score distributions for each group side by side using box plots. Which code correctly achieves this?
hard
A. df.boxplot(by='Score', column='Group')
B. df.groupby('Group').boxplot(column='Score')
C. df.plot.box(by='Group', column='Score')
D. df.boxplot(column='Score', by='Group')

Solution

  1. Step 1: Understand grouping in boxplot()

    To compare groups side by side, use boxplot() with column as the numeric data and by as the grouping column.
  2. Step 2: Evaluate each option

    df.boxplot(column='Score', by='Group') correctly uses column='Score' and by='Group'. df.groupby('Group').boxplot(column='Score') tries to group then boxplot, which returns multiple plots, not side by side. df.plot.box(by='Group', column='Score') uses invalid syntax. df.boxplot(by='Score', column='Group') swaps column and by incorrectly.
  3. Final Answer:

    df.boxplot(column='Score', by='Group') -> Option D
  4. Quick Check:

    Use column=score and by=group for grouped boxplots [OK]
Hint: Use df.boxplot(column='Score', by='Group') for grouped box plots [OK]
Common Mistakes:
  • Using groupby().boxplot() which plots separately
  • Swapping column and by arguments
  • Using invalid plot syntax
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