如何从groupby()绘制?

时间:2017-02-27 20:48:10

标签: python pandas matplotlib

我使用DataFrame函数分析了groupby()。我想绘制结果。

这是一段代码:

df = df[(df['Type'] == 'u')]
df['Date'] =  pd.to_datetime(df['Date'], format='%d-%m-%Y')
df['Month'] = df['Date'].dt.month 
mean_prices = df.groupby((['Suburb','Rooms', 'Month']))['Price'].agg(np.mean).round()
mean_prices

结果看起来像:

Suburb              Rooms  Month
    Abbotsford          1      5         432250.0
                               7         470000.0
                               8         441500.0
                               10        300000.0
                               11        490000.0
                               12        481000.0
                        2      5         970000.0
                               6         500000.0
                               10        542000.0
                               11        725000.0
                               12        760000.0
    Aberfeldie          1      12        380000.0
                        2      5         456000.0
                               11        373000.0
                        3      9         726000.0
                               12        845000.0
    Airport West        2      7         468250.0

我希望广告agg() np.maxnp.min并为每个'郊区制作一个箱线图。应显示每个郊区的最大值和平均值的值。怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用聚合来应用多个这样的函数:

mean_prices = df.groupby((['Suburb','Rooms', 'Month']))['Price'].aggregate({'Mean': np.mean, 'Min': np.min, 'Max': np.max}).round().reset_index()
mean_prices.head()

你得到:

    Suburb    Rooms Month   Mean    Min         Max
0   Abbotsford  1   5   432250.0    423500.0    441000.0
1   Abbotsford  1   7   470000.0    470000.0    470000.0
2   Abbotsford  1   8   441500.0    426000.0    457000.0
3   Abbotsford  1   10  300000.0    300000.0    300000.0
4   Abbotsford  1   11  490000.0    480000.0    500000.0