0

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)
3
  • @BradSolomon No, the year variable must be equal to 26 because you are including the 40th year as the first entry and the 65th year as the final entry Commented Dec 1, 2017 at 3:43
  • 1
    I follow, sorry. The semantics of the subject matter would say this is a 25 year investment. Commented Dec 1, 2017 at 3:46
  • That's correct. Commented Dec 1, 2017 at 3:49

4 Answers 4

1

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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.
  • The important term here is 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.

1 Comment

Interesting, I have never encountered the .shape command. What is that doing to the dataframe? The assignment which I am completing assumes an exponential growth pattern rather than the normal 1+% like you've completed in the code above.
0
df = pd.DataFrame(roi_list,index=range(40,40+years),columns=['Principle'])

Comments

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)) 

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.