我正在创建一个索赔的运行计数,并希望在每个月初将计数重置为0。
我现在有这些数据帧:
DF1:
<表类>
日期
X
Y
tbody><<tr>2021-04-25 4 4 2021-04-26 0 0 2021-04-27 0 0 2021-04-28 56 53 2021-04-29 0 0 2021-04-30 1 0 2021-05-01 0 0 2021-05-02 5 0 2021-05-03 5 5 2021-05-04 0 0 表类>
尝试:
Groupby (df[日期].dt.Month) .agg (cumsum)
这应该可以为您工作:
df = pd.DataFrame([
('2021-04-25', 4, 4),
('2021-04-26', 0, 0),
('2021-04-27', 0, 0),
('2021-04-28', 56, 53),
('2021-04-29', 0, 0),
('2021-04-30', 1, 0),
('2021-05-01', 0, 0),
('2021-05-02', 5, 0),
('2021-05-03', 5, 5),
('2021-05-04', 0, 0),
], columns=['Date', 'X', 'Y',])
df['Date'] = pd.to_datetime(df['Date'])
# Create Cumulative Sums for X
df['X'] = (df['X'] - df['Y'].shift(fill_value=0)).cumsum()
result = pd.concat([
group.assign(X=(
group['X'] - group.loc[group['Date'].dt.is_month_start, 'X'].values[0]
if len(group.loc[group['Date'].dt.is_month_start]) > 0
else group['X']
))
for month, group in df.groupby(df['Date'].dt.month)
])
输出:
Date X Y
0 2021-04-25 4 4
1 2021-04-26 0 0
2 2021-04-27 0 0
3 2021-04-28 56 53
4 2021-04-29 3 0
5 2021-04-30 4 0
6 2021-05-01 0 0
7 2021-05-02 5 0
8 2021-05-03 10 5
9 2021-05-04 5 0