簡體   English   中英

根據Python DF中的其他2列計算滾動總和

[英]Calculating a rolling sum based on 2 other columns in Python DF

我希望創建下表中的3個月滾動限制。 該限制基於前綴和sic組合。 因此,當AB 1到12月時,我希望AB 1月的總和為12 +1 + 2

解決這個問題的最佳方法是什么? 我使用過.rolling,但不確定如何處理前綴/ sic更改。

作為參考,我在“滾動3個月限制”列中手動輸入了所需的答案。

+-------+--------+-----+--------+-----------------------+
| Month | prefix | sic | limits | Rolling 3 month Limit |
+-------+--------+-----+--------+-----------------------+
|     1 | AB     |   1 | 16.5   | 54.3                  |
|     2 | AB     |   1 | 22.6   | 68.2                  |
|     3 | AB     |   1 | 15.2   | 175.8                 |
|     4 | AB     |   1 | 30.4   | 360.2                 |
|     5 | AB     |   1 | 130.2  | 371                   |
|     6 | AB     |   1 | 199.6  | 262.5                 |
|     7 | AB     |   1 | 41.2   | 80.7                  |
|     8 | AB     |   1 | 21.7   | 61.2                  |
|     9 | AB     |   1 | 17.8   | 53.4                  |
|    10 | AB     |   1 | 21.7   | 53.4                  |
|    11 | AB     |   1 | 13.9   | 48.2                  |
|    12 | AB     |   1 | 17.8   | 56.9                  |
|     1 | AB     |  10 | 9.8    | 32.4                  |
|     2 | AB     |  10 | 9.8    | 134.2                 |
|     3 | AB     |  10 | 12.8   | 132.7                 |
|     4 | AB     |  10 | 111.6  | 276.9                 |
|     5 | AB     |  10 | 8.3    | 252.9                 |
|     6 | AB     |  10 | 157    | 244.6                 |
|     7 | AB     |  10 | 87.6   |                       |
+-------+--------+-----+--------+-----------------------+
import pandas as pd
d = {'Month':[1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7],
     'prefix':['AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB','AB'],
     'sic':[1,1,1,1,1,1,1,1,1,1,1,1,10,10,10,10,10,10,10],
     'limits':[16.5,22.6,15.2,30.4,130.2,199.6,41.2,21.7,17.8,21.7,13.9,17.8,9.8,9.8,12.8,111.6,8.3,157,97.6],}
df = pd.DataFrame(d)
df['Rolling 3 month Limit'] = ''

def calc_roll(m,p,s):
   if m < 10: months = [m,m+1,m+2]
   if m == 10: months = [12,1,2]
   if m == 12: months = [12,1,2]
   if m == 11: months = [11,12,1]
   f = df.loc[(df['Month'].isin(months)) & (df['prefix'] == p) & (df['sic'] == s)]
   if len(f) < 3: return ''
   else: return sum(f['limits'])
df['Rolling 3 month Limit'] = df.apply(lambda x: calc_roll(x['Month'], x['prefix'], x['sic']),axis=1)


#Output

     Month   prefix  sic  limits Rolling 3 month Limit
 0       1     AB    1    16.5                  54.3
 1       2     AB    1    22.6                  68.2
 2       3     AB    1    15.2                 175.8
 3       4     AB    1    30.4                 360.2
 4       5     AB    1   130.2                   371
 5       6     AB    1   199.6                 262.5
 6       7     AB    1    41.2                  80.7
 7       8     AB    1    21.7                  61.2
 8       9     AB    1    17.8                  53.4
 9      10     AB    1    21.7                  56.9
 10     11     AB    1    13.9                  48.2
 11     12     AB    1    17.8                  56.9
 12      1     AB   10     9.8                  32.4
 13      2     AB   10     9.8                 134.2
 14      3     AB   10    12.8                 132.7
 15      4     AB   10   111.6                 276.9
 16      5     AB   10     8.3                 262.9
 17      6     AB   10   157.0                      
 18      7     AB   10    97.6                      

我已編寫此代碼以獲取您希望的輸出。 讓我知道是否有任何疑問!

編輯:

您可以調整if len(f) < 2:以獲得准確的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM