displayname displayname - 2 months ago 7
Python Question

How can I plot my groupby() result?

I'm running a

groupby()
on my data like this:

user.groupby(["DOC_ACC_DT", "DOC_ACTV_CD"]).agg("sum")["SUM_DOC_CNT"]


which results in this grouped data:

DOC_ACC_DT DOC_ACTV_CD
2015-07-01 BR 1
PT 1
2015-07-02 BR 1
PT 1
2015-07-06 BR 1
PT 1
2015-07-08 BR 1
2015-07-09 AD 2
PT 1
2015-07-13 AD 50
BR 52
PT 1
2015-07-14 AD 6
BR 5
PT 1
2015-07-16 BR 1
PT 1
2015-07-23 AD 13
BR 14
PT 3
2015-07-27 BR 1
PT 1


What I want to do now is simply plot by
DOC_ACTV_CD
. Please not that there are gaps between days so I'd have to fill in zero-values between days where nothing happened e.g.

2015-07-23 AD 13
BR 14
PT 3
2015-07-25 BR 1
PT 1


would have to become

2015-07-23 AD 13
BR 14
PT 3
2015-07-24 AD 0
BR 0
PT 0
2015-07-25 AD 0
BR 1
PT 1


before I plot a time series for
AD
,
BR' and
PT` in one plot. What's the quickest way to do this?

Answer

You can use:

df = user.groupby(["DOC_ACC_DT", "DOC_ACTV_CD"]).agg("sum")["SUM_DOC_CNT"]
df.unstack().resample('D').replace(np.nan,0).plot()