Question Details

No question body available.

Tags

pandas dataframe matplotlib nan boxplot

Answers (1)

Accepted Answer Available
Accepted Answer
September 18, 2025 Score: 3 Rep: 267,198 Quality: Expert Completeness: 80%

You could convert the DataFrame to a list of lists (or list of arrays) without NaNs (with dropna) and pass the names separately. I'll do this here using a dictionary:

data = {c: df[c].dropna().tolist() for c in df}

{'9-1': [23, 27, 10, 23, 10, 50, 12, 10, 34, 28],

'9-2': [16.0, 18.0, 9.0, 30.0, 10.0, 13.0, 24.0, 29.0],

'9-3': [18.0, 20.0, 15.0, 19.0, 23.0, 8.0, 31.0],

'9-4': [18.0, 17.0, 8.0, 5.0, 29.0, 20.0, 27.0, 7.0, 16.0],

'9-5': [26, 33, 30, 15, 12, 23, 35, 22, 31, 24]}

plt.boxplot(data.values(), ticklabels=data.keys())

Or, as a one-liner:

plt.boxplot([df[c].dropna() for c in df], ticklabels=list(df))

And for the sake of completeness, this can of course be obtained easily with the pandas API:

df.plot.box()

Output:

matplotlib boxplot with NaNs from pandas DataFrame