简体   繁体   中英

In Pandas, following a grouby and a pd.cut, how do I rearrange the data frame to plot by each bin over time?

I have a dataframe with 50 data points per month. I'd like to run a groupby on the date, and then calculate the median value for each decile within each month. I've been able to accomplish this with the code below:

import numpy as np
import pandas as pd

datecol = pd.date_range('12/31/2018','12/31/2019', freq='M')
for ii in range(0,49):
        datecol = datecol.append(pd.date_range('12/31/2018','12/31/2019', freq='M'))
datecol = datecol.sort_values()
df = pd.DataFrame(np.random.randn(len(datecol), 1), index=datecol, columns=['Data'])

dfg = df.groupby([df.index, pd.qcut(df['Data'], 10)])['Data'].median()

Now I'd like to be able to rearrange the dataframe so each decile has its own column. My goal is to plot each decile over time.

You can do:

dfg.unstack(-1).plot()

output:

在此处输入图片说明

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