Franklin
Franklin

Reputation: 21

Python Groupby and Plot

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

Upvotes: 2

Views: 131

Answers (1)

Akshay Sapra
Akshay Sapra

Reputation: 496

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

Upvotes: 1

Related Questions