![](/img/trans.png)
[英]Python Seaborn: getting AttributeError: 'str' object has no attribute 'get' when trying to plot
[英]“AttributeError: 'DataFrameGroupBy' object has no attribute 'get'” when attempting to box plot grouped data in Seaborn's .boxplot()
我早些时候问过这个问题,但它被转移到这个问题上,因为它很相似,但是,这不是我想要的 output。 我知道您可以对箱线图进行分组,但箱线图本身应该是分开的。 我按工作日对一组数据进行分组(从 0 到 6,总共 7 组),我希望将这 7 个箱线图放在一个 plot 上。 我为此使用 Seaborn 的箱线图 function:
sns.boxplot(data=concentration_by_weekday, x = 'weekday', y='NO2', ax=ax[1,0]);
它应该返回 7 个箱线图,一周中的每一天一个。 dataframe 应该返回 7 个组(如果我使用.describe()
,它会返回)我得到这个错误: AttributeError: 'DataFrameGroupBy' object has no attribute 'get'
。
语法应该是正确的,但是,我使用的 dataframe ( concentration_by_weekday
) 是DataFrameGroupBy
object。 concentration_by_weekday
_by_weekday dataframe 按工作日分组如下:
concentration_by_weekday = df_data[data_mask_4].groupby('weekday')
正在使用的数据掩码只是我用于其他子图的一个(其他子图 plot 正确,但它们不是箱线图,因此数据掩码应该不是问题)。 我知道应该可以在一张图上使用 plot 多个箱线图,我只是不确定这是否是正确的语法。 我在下面包含了相关的代码片段:
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]);
这是显示的完整错误:
---------------------------------------------------------------------------
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此刻的变数。 请注意,使用describe()
时它确实按预期工作。
您可以让 seaborn 进行分组。 这是它的强项之一。 所以,直接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.