user11953618
user11953618

Reputation: 69

Using numpy present value function

I am trying to compute the present value using numpy's pv function in pandas dataframe. I also have 2 lists, one includes period [6,18,24] and other one includes pmt values [100,200,300]. Present value should be computed for each value in pmt list to each value in period list.

lets say in below table column values represents period and row represents pmt

Click to see the image

I am trying to compute the data values using a single line of code without writing multiple lines How can I do that?

Currently I hard coded the period as follows.

PRESENT_VALUE6 = np.pv(pmt=-PMT_REMAINING_PERIOD,rate=(INTEREST_RATE/12),nper=6,fv=0,when=0)
PRESENT_VALUE18 = np.pv(pmt=-PMT_REMAINING_PERIOD,rate=(INTEREST_RATE/12),nper=18,fv=0,when=0)
PRESENT_VALUE30 = np.pv(pmt=-PMT_REMAINING_PERIOD,rate=(INTEREST_RATE/12),nper=30,fv=0,when=0)

I want the python to iterate the nper from the list, currently when I do that it produces the following not the expected result

Click to see the image

Expected result is

Click to see the image

Upvotes: 1

Views: 647

Answers (1)

Code Different
Code Different

Reputation: 93181

I don't know what interest rate you used in your example, I set it to 10% below:

INTEREST_RATE = 0.1

# Build a Cartesian product between PMT and Period
pmt = [100, 200, 300]
period = [6, 18, 24]
df = pd.DataFrame(product(pmt, period), columns=['PMT', 'Period'])

# Calculate the PV
df['PV'] = np.pv(INTEREST_RATE / 12, nper=df['Period'], pmt=-df['PMT'])

# Final pivot
df.pivot(index='PMT', columns='Period')

Result:

                 PV                          
Period           6            18           24
PMT                                          
100      582.881717  1665.082618  2167.085483
200     1165.763434  3330.165236  4334.170967
300     1748.645151  4995.247853  6501.256450

Upvotes: 1

Related Questions