简体   繁体   English

“AttributeError: 'DataFrameGroupBy' object has no attribute 'get'” 当试图在 Seaborn 的.boxplot() 中对 plot 分组数据进行装箱时

[英]“AttributeError: 'DataFrameGroupBy' object has no attribute 'get'” when attempting to box plot grouped data in Seaborn's .boxplot()

I asked this earlier but it was moved to this question as it was similar, however, this isn't the output I wanted.我早些时候问过这个问题,但它被转移到这个问题上,因为它很相似,但是,这不是我想要的 output。 I know you can group boxplots, but the boxplots themselves should be separate.我知道您可以对箱线图进行分组,但箱线图本身应该是分开的。 I'm grouping a bunch of data by weekday (from 0 to 6, so seven groups in total) and I want to have those 7 boxplots on one plot.我按工作日对一组数据进行分组(从 0 到 6,总共 7 组),我希望将这 7 个箱线图放在一个 plot 上。 I'm using Seaborn's boxplot function for this:我为此使用 Seaborn 的箱线图 function:

sns.boxplot(data=concentration_by_weekday, x = 'weekday', y='NO2', ax=ax[1,0]);

It should be returning 7 boxplots, one for each day of the week.它应该返回 7 个箱线图,一周中的每一天一个。 The dataframe should return 7 groups (which it does, if I use .describe() ) I instead get this error: AttributeError: 'DataFrameGroupBy' object has no attribute 'get' . dataframe 应该返回 7 个组(如果我使用.describe() ,它会返回)我得到这个错误: AttributeError: 'DataFrameGroupBy' object has no attribute 'get'

The syntax should be correct, however, the dataframe I am using ( concentration_by_weekday ) is a DataFrameGroupBy object.语法应该是正确的,但是,我使用的 dataframe ( concentration_by_weekday ) 是DataFrameGroupBy object。 The concentration_by_weekday dataframe is grouped by weekday as follows: concentration_by_weekday _by_weekday dataframe 按工作日分组如下:

concentration_by_weekday = df_data[data_mask_4].groupby('weekday')

The data mask being used is just one that I've used for my other subplots (the other ones plot correctly, but they aren't boxplots, so it shouldn't be an issue with the data mask).正在使用的数据掩码只是我用于其他子图的一个(其他子图 plot 正确,但它们不是箱线图,因此数据掩码应该不是问题)。 I know it should be possible to plot multiple boxplots on one graph, I'm just not sure if this is the right syntax.我知道应该可以在一张图上使用 plot 多个箱线图,我只是不确定这是否是正确的语法。 I've included the relevant code snippet below:我在下面包含了相关的代码片段:

data_mask_4 = (df_data['year'] != 2020)

concentration_by_weekday = df_data[data_mask_4].groupby('weekday')
#concentration_by_year = df_data[data_mask_4].groupby('year')
#concentration_by_year = concentration_by_year[['NO2', 'year']]

fig, ax = plt.subplots(ncols=2, nrows = 2, figsize=(17, 7))

#... plotting line graphs on [0,0] and [0,1] ...

sns.boxplot(data=concentration_by_weekday, x = 'weekday', y = 'NO2', ax=ax[1,0]);
sns.boxplot(data=concentration_by_year, x='weekday', y='NO2', ax=ax[1,1]);

Here is the full error that is displayed:这是显示的完整错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-67-cd5ffc1652e3> in <module>
     23 ax[0,1].plot(df_aq_quartiles_month['75%']);
     24 
---> 25 sns.boxplot(data=concentration_by_weekday, x = 'weekday', y = 'NO2', ax=ax[1,0]);
     26 #sns.boxplot(data=concentration_by_year, x='weekday', y='NO2', ax=ax[1,1]);

D:\Users\adcar\anaconda3\lib\site-packages\seaborn\categorical.py in boxplot(x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth, whis, ax, **kwargs)
   2239     plotter = _BoxPlotter(x, y, hue, data, order, hue_order,
   2240                           orient, color, palette, saturation,
-> 2241                           width, dodge, fliersize, linewidth)
   2242 
   2243     if ax is None:

D:\Users\adcar\anaconda3\lib\site-packages\seaborn\categorical.py in __init__(self, x, y, hue, data, order, hue_order, orient, color, palette, saturation, width, dodge, fliersize, linewidth)
    441                  width, dodge, fliersize, linewidth):
    442 
--> 443         self.establish_variables(x, y, hue, data, orient, order, hue_order)
    444         self.establish_colors(color, palette, saturation)
    445 

D:\Users\adcar\anaconda3\lib\site-packages\seaborn\categorical.py in establish_variables(self, x, y, hue, data, orient, order, hue_order, units)
    141             # See if we need to get variables from `data`
    142             if data is not None:
--> 143                 x = data.get(x, x)
    144                 y = data.get(y, y)
    145                 hue = data.get(hue, hue)

D:\Users\adcar\anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in __getattr__(self, attr)
    578 
    579         raise AttributeError(
--> 580             f"'{type(self).__name__}' object has no attribute '{attr}'"
    581         )
    582 

AttributeError: 'DataFrameGroupBy' object has no attribute 'get'

And here is an output of the dataframe itself, if I use concentration_by_weekday['NO2', 'weekday'].head() -- the dataframe has 33 columns so it's too large to show the whole thing, and I only need these two variables from it at the moment. And here is an output of the dataframe itself, if I use concentration_by_weekday['NO2', 'weekday'].head() -- the dataframe has 33 columns so it's too large to show the whole thing, and I only need these two此刻的变数。 Note that it does work as expected when using describe() .请注意,使用describe()时它确实按预期工作。

You could let seaborn do the grouping.您可以让 seaborn 进行分组。 It is one of its fortes.这是它的强项之一。 So, directly sns.boxplot(data=df, x='weekday', y='NO2') .所以,直接sns.boxplot(data=df, x='weekday', y='NO2')

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

N = 100
df_data = pd.DataFrame({'year': np.random.randint(2015, 2021, N),
                        'weekday': np.random.randint(0, 7, N),
                        'NO2': np.random.uniform(5, 40, N)})
year_filter = df_data['year'] != 2020

fig, axes = plt.subplots(ncols=2, squeeze=False)
sns.boxplot(data=df_data[year_filter], x='weekday', y='NO2', ax=axes[0, 0])
sns.boxplot(data=df_data[year_filter], x='year', y='NO2', ax=axes[0, 1])
plt.tight_layout()
plt.show()

示例图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM