How to plot a boxplot for each column in a DataFrame? [duplicate]
Asked Answered
D

2

13

I have a DataFrame df of multiple columns and I would like to create a boxplot for each column using matplotlib.

df.info() output of my DataFrame below for reference

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 9568 entries, 0 to 9567
Data columns (total 5 columns):
Ambient Tempreature    9568 non-null float64
Exhaust Vacuum         9568 non-null float64
Ambient Pressure       9568 non-null float64
Relative Humidity      9568 non-null float64
PE                     9568 non-null float64
dtypes: float64(5)
memory usage: 373.8 KB
Deenadeenya answered 9/8, 2018 at 23:59 Comment(0)
D
21

If you want to create a separate plot per column, then you can iterate over each column and use plt.figure() to initiate a new figure for each plot.

import matplotlib.pyplot as plt

for column in df:
    plt.figure()
    df.boxplot([column])

If you want to just put all columns into the same boxplot graph then you can just use df.plot(kind='box')

Deenadeenya answered 9/8, 2018 at 23:59 Comment(2)
This produces an error KeyError: "None of [Index(['Name'], dtype='object')] are in the [columns]". Any thoughts?Nettles
Do you have any spaces in your column names? Can you change them to - or _ and try again? if not sufficient to resolve your issue, please post your df and I'll take a look.Deenadeenya
O
3

You can get rid of KeyError: "None of [Index(['Name'], dtype='object')] are in the [columns]" by excluding the object dtype variables from your dataframe

df1=df.select_dtypes(exclude=['object'])

and then run

import matplotlib.pyplot as plt

for column in df1:
        plt.figure(figsize=(17,1))
        sns.boxplot(data=df1, x=column)
Oarfish answered 14/9, 2021 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.