Skip to main content
 首页 » 编程设计

python之通过对列中的前一个值求和并相乘,在 Pandas 中创建一个列

2025年01月19日8pander-it

我认为我的问题最好用一个例子来说明。

以下是我作为输入的列。

Month       Monthly_Interest    Payment_Shortfall      Amount_In_Arrears 
1               0.1             20                      
2               0.1             100                     
3               0.1             105                     
4               0.1             50                      
5               0.1             5                       
6               0.1             30                      
7               0.1             70                      

这是我需要的输出。

Month       Monthly_Interest    Payment_Shortfall      Amount_In_Arrears 
1               0.1             20                     20.00 
2               0.1             100                    122.00 
3               0.1             105                    239.20 
4               0.1             50                     313.12 
5               0.1             5                      349.43 
6               0.1             30                     414.38 
7               0.1             70                     525.81 

这是一笔每月因 Payment_Shortfall 而少付的贷款。

Payment_Shortfall 每个月累积形成 Amount_In_Arrears。

利息根据上个月的 Amount_In_Arrears 收取,并添加到当月的 Amount_In_Arrears。

换句话说:Amount_In_Arrears = Payment_Shortfall + Amount_In_Arrears(上个月)+ Amount_In_Arrears(上个月)* Monthly_Interest

例如,对于第 2 个月:Amount_in_Arrears = 100 + 20 + 20 * 0.1 = 122 对于第 3 个月:Amount_in_Arrears = 105 + 122 + 122 * 0.1 = 239.20 >

我一直在尝试在 Pandas 中执行此操作,但我似乎无法弄清楚。我需要将它应用于相当大的数据框(大约 300 万行),因此效率是一个考虑因素。

我的数据有很多这样的贷款,所以我也需要重置解决方案(可能使用 groupby() 函数),但是,我认为最好一次处理一个问题。

非常感谢您的帮助!

请您参考如下方法:

据我所知,在普通 Pandas 中没有有效的方法来计算它(但如果有的话,我非常想知道)。我知道有两个选项可以在 python 中为此类任务进行快速计算,cython (特别注释编译为 c 的 python)和 numba (纯 Python 的即时编译器)。

由于我对cython了解不多,所以我会给你numba的例子。

对于 numba,您可以使用 numba 装饰器编写一个单独的函数。 Numba 与 numpy 配合得很好,但不了解 pandas,因此我们必须将 numpy 数组提供给函数:

@numba.jit(nopython=True) 
def compute_total_amount(interest, amount): 
  n = len(interest) 
  total_amount = np.empty(n) 
  total_amount[0] = amount[0] 
  for i in range(1,n): 
    total_amount[i] = total_amount[i-1]*(1+interest[i])+amount[i] 
  return total_amount 
 
df['Amount_In_Arrears'] = compute_total_amount(df['Monthly_Interest'].values,\ 
                                               df['Payment_Shortfall'].values) 
 
   Payment_Shortfall  Monthly_Interest  Amount_In_Arrears 
0                 20               0.1           20.00000 
1                100               0.1          122.00000 
2                105               0.1          239.20000 
3                 50               0.1          313.12000 
4                  5               0.1          349.43200 
5                 30               0.1          414.37520 
6                 70               0.1          525.81272 

编辑:添加了 numba 与纯 python(100 万行)的计时

df= pd.DataFrame({'Payment_Shortfall':np.random.random(10**6)*10})) 
df['Monthly_Interest'] = -0.01 
 
def compute_total_amount_python(interest, amount): 
  n = len(interest) 
  total_amount = np.empty(n) 
  total_amount[0] = amount[0] 
  for i in range(1,n): 
    total_amount[i] = total_amount[i-1]*(1+interest[i])+amount[i] 
  return total_amount 
 
%timeit compute_total_amount_python(df['Monthly_Interest'].values, df['Payment_Shortfall'].values) 
729 ms ± 36.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 
 
%timeit compute_total_amount(df['Monthly_Interest'].values, df['Payment_Shortfall'].values) 
2.7 ms ± 62.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)