【问题标题】:How to plot a boxplot for each column in a DataFrame? [duplicate]如何为 DataFrame 中的每一列绘制箱线图? [复制]
【发布时间】:2019-01-17 12:30:15
【问题描述】:

我有一个包含多列的 DataFrame df,我想使用 matplotlib 创建一个 boxplot为每一列

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

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    您可以通过从数据框中排除对象 dtype 变量来摆脱 KeyError: "None of [Index(['Name'], dtype='object')] are in the [columns]"

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

    然后运行

    import matplotlib.pyplot as plt
    
    for column in df1:
            plt.figure(figsize=(17,1))
            sns.boxplot(data=df1, x=column)
    

    【讨论】:

      【解决方案2】:

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

      import matplotlib.pyplot as plt
      
      for column in df:
          plt.figure()
          df.boxplot([column])
      

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

      【讨论】:

      • 这会产生错误KeyError: "None of [Index(['Name'], dtype='object')] are in the [columns]"。有什么想法吗?
      • 列名中有空格吗?您可以将它们更改为-_ 并重试吗?如果不足以解决您的问题,请发布您的df,我会看看。
      猜你喜欢
      • 2018-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-06-30
      • 2021-01-30
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多