Reputation: 45
I'm trying to change the dataframe printout so that it starts numbering the first dataframe entry at 40 instead of 0.
import pandas as pd
import numpy as np
import glob
import datetime as dt
import math
principle = 50000 # Amount initially invested
rate_of_return = .076 # Return on investment or the interest rate
years = 26 # Assuming starting at age 40 and end at age 65
roi_list = []
for i in range(years):
roi_list.append(principle)
principle = principle*(math.exp(rate_of_return))
df = pd.DataFrame(roi_list) # Creates a dataframe containing the roi_list values
print(df)
Upvotes: 0
Views: 61
Reputation: 40908
Since you are using pandas, you may want to consider a vectorized solution. This is what pandas is designed for.
years = np.arange(26)
r = .076 # 7.6% compounded annual growth rate
principle = 50000
s = pd.Series(principle * ((1+r)**years),
index=np.add(years, 40))
print(s.head())
40 50000.000
41 53800.000
42 57888.800
43 62288.349
44 67022.263
dtype: float64
Walkthrough:
years = np.arange(26)
creates a NumPy array that starts at 0 and ends at 25.r
is our annualized growth rate. You said in your comment that you're assuming an exponential growth rate, but this doesn't look to be the case. You're currently using an annualized growth rate of e raised to .076 (math.exp(r)
), which is 7.89% annualized. This is a constant growth rate, not an exponential one.principle * ((1+r)**years)
. Just taking (1+r)**years
would show you the growth of $1 compounded annually at 7.6%. Multiply (the entire array by) your beginning principle gets you the hypothetical growth of that principal.Upvotes: 1
Reputation: 3174
pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False) takes an index
argument to provide index information.
index : Index or array-like
Index to use for resulting frame. Will default to np.arange(n) if no indexing information part of input data and no index provided
So this will do the job:
df = pd.DataFrame(roi_list, index=np.arange(40, 40 + years))
Upvotes: 1
Reputation: 1731
import pandas as pd
import numpy as np
import glob
import datetime as dt
import math
principle = 50000 # Amount initially invested
rate_of_return = .076 # Return on investment or the interest rate
years = 26 # Assuming starting at age 40 and end at age 65
roi_list = []
for i in range(years):
roi_list.append(principle)
principle = principle*(math.exp(rate_of_return))
yearlist = []
for i in range(years):
yearlist.append(i + 40)
dates = pd.DataFrame(yearlist)
df = pd.DataFrame(roi_list, index = dates) # Creates a dataframe containing the roi_list values
print(df)
Is this how you want it?
Upvotes: 0
Reputation: 5844
df = pd.DataFrame(roi_list,index=range(40,40+years),columns=['Principle'])
Upvotes: 0