6
$\begingroup$

I'm having trouble graphing Pandas grouped data in Bokeh.

company_id    company_score    date_submitted    company_region
AA            .07              1/1/2017          NW
AB            .08              1/2/2017          NE
CD            .0003            1/18/2017         NW

I've successfully grouped the data by company_id, and calculated the simple moving average of company_score using 10 days / inputs.

score_by_company = df['company_score'].groupby(df['company_id']).rolling(10).mean()

company_id    index           SMA     
BBG000B9WMF7  7613            NaN
              1911663         NaN
              1911664         NaN
              1911665         NaN
              1911666         NaN
              1911667         NaN
              1911668         NaN
              1911669         NaN
              1911670         NaN
              1911671    0.000002
              1911672    0.000002
              etc.       etc.

How can I translate this data into a time series graph, with each company_id being its own line?

I feel like I need to create a data dictionary off of the grouped data, but I'm not sure if that's the right approach.

$\endgroup$
3
  • $\begingroup$ You want to plot company score and time? $\endgroup$
    – Aditya
    Commented Apr 4, 2018 at 19:21
  • $\begingroup$ Correct, I'm having difficulty going from grouped data to a graph. $\endgroup$ Commented Apr 4, 2018 at 21:48
  • $\begingroup$ When you have grouped data, they are not at the same index level, so you need to loop $\endgroup$
    – Aditya
    Commented Apr 5, 2018 at 1:54

1 Answer 1

2
$\begingroup$

You can make the plots by looping over the groups from groupby...

Or this should also work..

import matplotlib
import matplotlib.pyplot as plt
df['company_score'].groupby(df['company_id']).rolling(10).mean().unstack().plot()

See this for more information on plotting with pandas dataframes and this for looping over a groupby-object. Finally this link is super helpful.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.