通过滚动分组表示熊猫

时间:2018-04-19 18:33:56

标签: python pandas

我需要计算按天分组的数据的滚动均值。

ID  BC_DT       BB_3M_DEFAULT_PROB  REGION
AA  27-Mar-18   0                   Chicago
BB  27-Mar-18   0.000002            Chicago
CC  27-Mar-18   0.000003            Chicago
DD  27-Mar-18   0.000006            Chicago
AA  28-Mar-18   0                   Dallas
BB  28-Mar-18   0                   New York
CC  28-Mar-18   0.000005            Chicago
DD  28-Mar-18   0.000004            Kansas City
AA  29-Mar-18   0.000002            Chicago
BB  29-Mar-18   0.000002            Chicago
CC  29-Mar-18   0.000002            Kansas City
DD  29-Mar-18   0.000005            Chicago
AA  30-Mar-18   0.000002            Kansas City
BB  30-Mar-18   0.019309            New York
CC  30-Mar-18   0.000004            Dallas
DD  30-Mar-18   0.000007            Chicago
AA  31-Mar-18   0.000002            Dallas
BB  31-Mar-18   0.000003            Dallas

#Set BC_DT to datetime format.
df_0['BC_DT'] = pd.to_datetime(df_0['BC_DT'])
#Set BC_DT to be index.
df_1 = df_1.set_index('BC_DT')
#Sort index so we're in chronological order.
df_1 = df_1.sort_index(axis=0, ascending=True)
#Calculate a daily mean grouping by day.
df_1['3M_DailyMean'] = df_1.groupby(df_1.index)['BB_3M_DEFAULT_PROB'].mean()

在这里,我被绊倒了。

df_1['3M_SMA'] = df_1.groupby(df_1.index)['BB_3M_DEFAULT_PROB'].rolling(10, center=False).mean().reset_index(0, drop=True)

我试图按日期分组,然后创建一个跨度为10天的滚动平均值。有人看到我绊倒的地方吗?

我希望滚动的意思可以在几天内运行,因此每个日期应该具有相同的意思。'

2017-01-01         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02         NaN
2017-01-02    0.001851
2017-01-02    0.001592
2017-01-02    0.001592
2017-01-02    0.001593
2017-01-02    0.001592
2017-01-02    0.001592
2017-01-02    0.001592
2017-01-02    0.000003
2017-01-02    0.000005
2017-01-02    0.000005
2017-01-02    0.000005
2017-01-02    0.000004
2017-01-02    0.000004

1 个答案:

答案 0 :(得分:1)

尝试创建新的数据框,并尝试使用pd.TimeGrouper。

# index is a datetime
df.set_index('BC_DT', inplace=True)

# group by day
groups  = df.groupby(pd.TimeGrouper('D'))

# new dataframe
df2 = pd.DataFrame()

# dataframe with days and sum of 'BB_3M_DEFAULT_PROB' per day
df2['daily_sum'] = groups['BB_3M_DEFAULT_PROB'].sum()

# add rolling mean to dataframe
df2['rolling_mean'] = df2['BB_3M_DEFAULT_PROB'].rolling(10,center=False).mean()