A box plot draft that deleted possible outliers

Code review · Basic · §8.4 Box plots and possible outliers

This AI draft compares the weights (kg) of parcels that arrived at the dorm mailroom (Dorm) and the office mailbox (Office) with box plots. The draft drew the box plots from kept, a DataFrame with the parcels heavier than the upper bound removed.

Do not delete possible outliers before you check the original records. Change one line so that the box plots are drawn with all the parcels.

Target figure for A box plot draft that deleted possible outliers
Target figure

Code to fix

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

desks = ["Dorm"] * 8 + ["Office"] * 8
kg = [2, 3, 3, 4, 4, 5, 6, 21, 1, 2, 2, 3, 5, 6, 7, 9]
parcels = pd.DataFrame({"desk": desks, "kg": kg})

q1 = parcels["kg"].quantile(0.25)
q3 = parcels["kg"].quantile(0.75)
upper = q3 + 1.5 * (q3 - q1)
kept = parcels[parcels["kg"] <= upper]  # remove outliers

fig, ax = plt.subplots()
sns.boxplot(data=kept, x="desk", y="kg", ax=ax)
ax.set_ylabel("Weight (kg)")
plt.show()

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.