I've created a function, which returns DataFrame with 3 columns - Year, Principal amount and End balance. It works fine, however the format number of End balance is weird, but I'm not sure, what I've done wrong.
If the input is investment_calculator(15000,7,10). It returns End balance in correct format:
However if it exceeds particular amount, e.g. investment_calculator(15000,10,10). The format of End balance is different, although nothing's changed:
Function:
def investment_calculator(amount,years,interest):
"""
Investment calculator calculates returned amount.
Args:
amount: monthly invested amount
years: number of years
interest: average yearly interest
Returns:
DataFrame
"""
col_names = ['Year', 'Principal', 'End balance']
df = pd.DataFrame(columns = col_names)
i = 1
while i <= years:
invested_amount = (amount * 12) * i
year = datetime.datetime.now().year (i - 1)
if i == 1:
investment = (amount * 12) * (1 (interest / 100))
new_row = {'Year':year, 'Principal':invested_amount, 'End balance':investment}
df = df.append(new_row, ignore_index=True)
else:
investment = (df.iloc[-1,2] (amount * 12)) * (1 (interest / 100))
new_row = {'Year':year, 'Principal':invested_amount, 'End balance':investment}
df = df.append(new_row, ignore_index=True)
i = 1
return df
Any ideas, what's wrong, please?
CodePudding user response:
Apparently pandas switches to scientific number notation when the numbers are sufficiently large. Your code does not contain the actual printing. When you print the results, format them as suitable, possibly by using f-strings: f"{df['End Balance']:.2f}".
CodePudding user response:
I am sure you know your values are simply displayed using scientific notation. If its bother you, you can set a different default format, for instance:
pd.options.display.float_format = '{:.2f}'.format


