我有一个数据框,其中包含年份值、乘客数据及其来源国家/地区。该国在数据框架上的想法仍然相同。
我正在尝试绘制箱线图。其中,国家/地区:“美国”位于 x 轴上,所有年份的乘客数据显示在 y 轴上。
但我得到:AttributeError:'Series'对象没有属性'boxplot'
from matplotlib import pyplot as plt
import pandas as pd
df = pd.read_csv('CityPairs.csv')
filt = (df.Country == 'USA')
df = df[filt]
reqcol = ['Year', 'Country', 'Passengers_Total']
df = df[reqcol]
groupbyCountry = df.groupby(['Year', 'Country'])['Passengers_Total'].sum()
groupbyCountry
groupbyCountry.boxplot(column = 'Passengers_Total', by = 'Country')
任何帮助将不胜感激
最佳答案
使用reset_index
或 DataFrame 的参数 as_index=True
:
groupbyCountry = df.groupby(['Year', 'Country'])['Passengers_Total'].sum().reset_index()
或者:
groupbyCountry = df.groupby(['Year', 'Country'], as_index=False)['Passengers_Total'].sum()
如果仅绘制一个类别(美国
),您可以简化代码:
groupbyCountry.boxplot(column = 'Passengers_Total')
关于python - 来自 python 值表的箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46176958/