简体   繁体   English

熊猫数据帧净现值向量化(函数向量化)

[英]Pandas dataframe net present value vectorization (function vectorization)

I have the following dataframe: 我有以下数据框:

import numpy as np
import pandas as pd
dates = pd.date_range(start='2014-01-01',end='2018-01-01',  freq='Y')
df = pd.DataFrame(5*np.eye(4,), index=dates, columns=['Var1', 'Var2', 'Var3', 'Var4'])
print(df)

            Var1  Var2  Var3  Var4
2014-12-31   5.0   0.0   0.0   0.0
2015-12-31   0.0   5.0   0.0   0.0
2016-12-31   0.0   0.0   5.0   0.0
2017-12-31   0.0   0.0   0.0   5.0

I would like to compute the NPV value of each variable for the years 2014 and 2015 for 3 years. 我想计算2014年和2015年的3年每个变量的NPV值。

Right now I know how to obtain the present value for one variable and one row at the time: 现在,我知道如何同时获取一个变量和一行的当前值:

Var1_2014     = df.loc['2014':'2016','Var1'].tolist()
NPV_Var1_2014 = np.npv(0.7,[0]+Var1_2014) 

However I do not know how to vectorize the function to compute directly the entire column. 但是,我不知道如何对函数进行向量化以直接计算整个列。 I would like to obtain something like that: 我想获得类似的东西:

             Var1  Var2  Var3  Var4  Var1_NPV
2014-12-31   5.0   0.0   0.0   0.0      a
2015-12-31   0.0   5.0   0.0   0.0      b
2016-12-31   0.0   0.0   5.0   0.0     Nan
2017-12-31   0.0   0.0   0.0   5.0     Nan

where I could say something like df['Var1_NPV']= npv('Var1',duration=3years,discount_rate=0.7) 在这里我可以说df['Var1_NPV']= npv('Var1',duration=3years,discount_rate=0.7)

Any idea on how I could vectorize that function efficiently? 关于如何有效地矢量化该功能的任何想法吗?

Many thanks, 非常感谢,

I find a solution with apply and offset: 我找到了一个带有apply和offset的解决方案:

def give_npv(date,df,var,wacc):   
    date2 = date + pd.DateOffset(years=2)  
    data    = df.loc[date:date2,var].tolist()
    NPV_var = np.npv(wacc,[0]+data) 
    return NPV_var


df['index2'] = df.index
df['test'] = df.apply(lambda x: give_npv(x['index2'],df,'Var2',0.07) ,axis=1 ) 
print(df)

            Var1  Var2  Var3  Var4     index2      test
2014-12-31   5.0   0.0   0.0   0.0 2014-12-31  4.367194
2015-12-31   0.0   5.0   0.0   0.0 2015-12-31  4.672897
2016-12-31   0.0   0.0   5.0   0.0 2016-12-31  0.000000
2017-12-31   0.0   0.0   0.0   5.0 2017-12-31  0.000000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM