我有
num city inc pop edu crime cult
1,0 Moscow 29343,00 8683,00 0,00 10,40 0,00
2,0 Paris 25896,00 17496,00 0,00 10,20 1,00
3,0 London 21785,00 15063,00 0,00 14,20 1,00
4,0 Berlin 20000,00 70453,00 1,00 18,00 1,00
我尝试使用
进行box-whisker
绘图
desire_salary = (df[(df['inc'] <= int(salary_people))])
result = desire_salary.pivot_table('city', 'cult', aggfunc='count')
result.plot.boxplot(ax=axarr[1, 1])
但我得AttributeError: 'SeriesPlotMethods' object has no attribute 'boxplot'
怎么了?
答案 0 :(得分:1)
此处的问题是desire_salary.pivot_table('city', 'cult', aggfunc='count')
只选中了一个值cult
。 pivot_table
的标准行为是当pivot_table只有一个值/一列时返回series
。但是,series
对象没有boxplot
方法,因此我们必须先将其更改为数据框。
有两种方法可以将系列更改为数据帧:
1)在创建list
pivot_table
参数中输入pivot_table
(即使只有一个值)
result = df.pivot_table(index='city', values=['cult'], aggfunc='count')
df2.boxplot()
2)在to_frame()
series
后调用pivot_table
方法
result = desire_salary.pivot_table(values = 'cult', index = 'city', aggfunc='count')
result.to_frame().boxplot()