Python Groupby和Plot

时间:2018-04-17 01:00:10

标签: python pandas plot group-by

通过以下分组,我如何最终对数据进行分组,以便在迭代每个符号和交换时绘制价格(x轴)和大小(y轴)?感谢。

df_group = df.groupby(['symbol','exchange','price'])["size"].sum()

symbol  exchange  price  
AAPL    ARCA      154.630     800
                  154.640     641
                  154.650     100
                  154.660     300
                  154.670     400
                  154.675     100
                  154.680     300
                  154.690    1390
                  154.695     100
                  154.700     360
                  154.705     100
                  154.710     671
                  154.720     190
                  154.725     100
                  154.730     400
...
XOM     PSX       80.67                                                 1300
                  80.68                                                 2721
                  80.69                                                 1901
                  80.7                                                   700
                  80.71                                                  800
                  80.72                                                  200
                  80.73                                                  700
                  80.74                                                  500
                  80.75                                                  600
                  80.76                                                  300
                  80.77                                                  900
                  80.78                                                  100
                  80.79                                                 1000
                  80.8                                                  1000
symbol  exch      price    sizesizesizesizesizesizesizesizesizesizesizesi...

1 个答案:

答案 0 :(得分:1)

您可以使用聚合函数

fun={'symbol':{'size':'count'} 
df_group = df.groupby(['symbol','exchange','price']).agg(fun).reset_index()
df_group.columns=df_group.columns.droplevel(1)
df_group