聚合和滚动特定窗口后如何应用自定义函数(使用应用方法)

我有这样的 DateFrame: 

period          payor       variance_charges

6/1/2018    LIABILITY PLANS 4631.6667

7/1/2018    LIABILITY PLANS -1125.8333

8/1/2018    LIABILITY PLANS -12688.3333

9/1/2018    LIABILITY PLANS -1657.5

10/1/2018   LIABILITY PLANS -14806.6667

11/1/2018   LIABILITY PLANS 13910.8333

12/1/2018   LIABILITY PLANS 12154.1667

6/1/2018    MEDICAID CMO    -39174.5817

7/1/2018    MEDICAID CMO    59504.5767

8/1/2018    MEDICAID CMO    13967.4883

9/1/2018    MEDICAID CMO    -158103.49

10/1/2018   MEDICAID CMO    -71191.9667

11/1/2018   MEDICAID CMO    -405366.1217

12/1/2018   MEDICAID CMO    -21637.05


在对付款人(列)进行汇总后,我想检查每个窗口(每个窗口上 3 行)有多少负值:


period      payor     variance_charges  count_neg

6/1/2018    LIABILITY PLANS 4631.6667   0

7/1/2018    LIABILITY PLANS -1125.8333  1

8/1/2018    LIABILITY PLANS -12688.3333 2

9/1/2018    LIABILITY PLANS -1657.5     3

10/1/2018   LIABILITY PLANS -14806.6667 3

11/1/2018   LIABILITY PLANS 13910.8333  2

12/1/2018   LIABILITY PLANS 12154.1667  1

6/1/2018    MEDICAID CMO    -39174.5817 1

7/1/2018    MEDICAID CMO    59504.5767  1

8/1/2018    MEDICAID CMO    13967.4883  1

9/1/2018    MEDICAID CMO    -158103.49  1

10/1/2018   MEDICAID CMO    -71191.9667 2

11/1/2018   MEDICAID CMO    -405366.12  3

12/1/2018   MEDICAID CMO    -21637.05   3


我试过下面的代码


df.sort_values(by = 'period', ascending=True)

df['count_neg'] = df.groupby(['payor'])['variance_charges'].transform(lambda x: x.rolling(6, min_periods=1).apply(lambda n: sum(n < 0 for n in x), raw = False))

使用上面的代码,我可以检查整个聚合有多少负值,而不考虑窗口。我得到的错误结果如下所示:


period      payor    variance_charges   count_neg

6/1/2018    LIABILITY PLANS 4631.6667   4

7/1/2018    LIABILITY PLANS -1125.8333  4

8/1/2018    LIABILITY PLANS -12688.3333 4

9/1/2018    LIABILITY PLANS -1657.5     4

10/1/2018   LIABILITY PLANS -14806.6667 4

11/1/2018   LIABILITY PLANS 13910.8333  4

12/1/2018   LIABILITY PLANS 12154.1667  4

6/1/2018    MEDICAID CMO    -39174.5817 5

7/1/2018    MEDICAID CMO    59504.5767  5

8/1/2018    MEDICAID CMO    13967.4883  5

请帮助解决这个问题。


动漫人物
浏览 168回答 1
1回答

开心每一天1111

您可以通过 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)&nbsp; &nbsp; &nbsp; &nbsp;period&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; payor&nbsp; variance_charges&nbsp; count_neg&nbsp; count_neg10&nbsp; &nbsp; 6/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4631.6667&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;01&nbsp; &nbsp; 7/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp; -1125.8333&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;12&nbsp; &nbsp; 8/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp;-12688.3333&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;23&nbsp; &nbsp; 9/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp; -1657.5000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;34&nbsp; &nbsp;10/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp;-14806.6667&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;35&nbsp; &nbsp;11/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp; 13910.8333&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;26&nbsp; &nbsp;12/1/2018&nbsp; LIABILITY PLANS&nbsp; &nbsp; &nbsp; &nbsp; 12154.1667&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;17&nbsp; &nbsp; 6/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; &nbsp;-39174.5817&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;18&nbsp; &nbsp; 7/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; &nbsp; 59504.5767&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;19&nbsp; &nbsp; 8/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; &nbsp; 13967.4883&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;110&nbsp; &nbsp;9/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; -158103.4900&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;111&nbsp; 10/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; &nbsp;-71191.9667&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;212&nbsp; 11/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; -405366.1200&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;313&nbsp; 12/1/2018&nbsp; &nbsp; &nbsp;MEDICAID CMO&nbsp; &nbsp; &nbsp; &nbsp;-21637.0500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python