如何为 DataFrame 中的每一列绘制箱线图?

hel*_*ode 8 python matplotlib pandas

我有一个数据帧df多个列的,我想创建一个boxplot 为每个列使用matplotlib

df.info() 下面我的 DataFrame 的输出以供参考

<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
Run Code Online (Sandbox Code Playgroud)

hel*_*ode 14

如果您想为每列创建一个单独的图,那么您可以遍历每一列并用于plt.figure()为每个图启动一个新图。

import matplotlib.pyplot as plt

for column in df:
    plt.figure()
    df.boxplot([column])
Run Code Online (Sandbox Code Playgroud)

如果您只想将所有列放入同一个箱线图,那么您可以使用 df.plot(kind='box')

  • 这会产生错误“KeyError:“[Index(['Name'], dtype='object')] 都不在 [columns] 中”。有什么想法吗? (2认同)