1 回答
TA贡献1836条经验 获得超13个赞
您可以通过 remove 简化您的功能for n in x:
f = lambda x: x.rolling(3, min_periods=1).apply(lambda n: sum(n < 0), raw = False)
df['count_neg1'] = df.groupby(['payor'])['variance_charges'].transform(f).astype(int)
print (df)
period payor variance_charges count_neg count_neg1
0 6/1/2018 LIABILITY PLANS 4631.6667 0 0
1 7/1/2018 LIABILITY PLANS -1125.8333 1 1
2 8/1/2018 LIABILITY PLANS -12688.3333 2 2
3 9/1/2018 LIABILITY PLANS -1657.5000 3 3
4 10/1/2018 LIABILITY PLANS -14806.6667 3 3
5 11/1/2018 LIABILITY PLANS 13910.8333 2 2
6 12/1/2018 LIABILITY PLANS 12154.1667 1 1
7 6/1/2018 MEDICAID CMO -39174.5817 1 1
8 7/1/2018 MEDICAID CMO 59504.5767 1 1
9 8/1/2018 MEDICAID CMO 13967.4883 1 1
10 9/1/2018 MEDICAID CMO -158103.4900 1 1
11 10/1/2018 MEDICAID CMO -71191.9667 2 2
12 11/1/2018 MEDICAID CMO -405366.1200 3 3
13 12/1/2018 MEDICAID CMO -21637.0500 3 3
添加回答
举报