简体   繁体   中英

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 eg

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM