gpt4 book ai didi

python - 通过滚动方式分组 pandas

转载 作者:行者123 更新时间:2023-12-01 09:31:31 27 4
gpt4 key购买 nike

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

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

最佳答案

尝试创建一个新的数据框并尝试使用 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()

关于python - 通过滚动方式分组 pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49927952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com