Python Groupby and Plot

Franklin Source

With the following groupby how can I ultimately group the data so that I can plot the price (x-axis) and size (y-axis) while iterating through every symbol and exchange? Thanks.

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...
pythonpandasplotgroup-by

Answers

answered 3 weeks ago Akshay Sapra #1

you can use aggregate functions

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

comments powered by Disqus