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$
  • $\begingroup$ You want to plot company score and time? $\endgroup$ – Aditya Apr 4 '18 at 19:21
  • $\begingroup$ Correct, I'm having difficulty going from grouped data to a graph. $\endgroup$ – ForsakenPlague Apr 4 '18 at 21:48
  • $\begingroup$ When you have grouped data, they are not at the same index level, so you need to loop $\endgroup$ – Aditya Apr 5 '18 at 1:54
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 for more information on plotting with pandas dataframes:

and for looping over a groupby-object:

This link is super helpful

| improve this answer | |
$\endgroup$
0
$\begingroup$

You can try to use http://holoviews.org/ This framework has useful tools which you can use in complicated visualization.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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