使用python根据贷款额计算emi后本金为零吗?

时间:2019-05-28 06:37:56

标签: python dataframe

我正在从事金融项目,我想为摊销时间表准备一个api。利息和付款运作良好,但是本金从零开始,我尝试了许多网站,但无法获得确切答案。谁能帮我?以下是我尝试过的python代码。预先感谢。

import pandas as pd
import numpy as np
from datetime import date

Interest_Rate = 10
Years = 5
Payments_Year = 12
Principal = 100000
Addl_Princ = 50
start_date = (date(2019,5,28))

pmt = np.pmt(Interest_Rate/Payments_Year, Years*Payments_Year, Principal)
per = 1

# Calculate the interest
ipmt = np.ipmt(Interest_Rate/Payments_Year, per, Years*Payments_Year, Principal)

# Calculate the principal
ppmt = np.ppmt(Interest_Rate/Payments_Year, per, Years*Payments_Year, Principal)

rng = pd.date_range(start_date, periods=Years * Payments_Year, freq='MS')
rng.name = "Payment_Date"
df = pd.DataFrame(index=rng, columns=['Payment', 'Principal', 'Interest'], dtype='float')
df.reset_index(inplace=True)
df.index += 1
df.index.name = "Period"

df["Payment"] = np.pmt(Interest_Rate/Payments_Year, Years*Payments_Year, Principal)
df["Principal"] = np.ppmt(Interest_Rate/Payments_Year, df.index, Years*Payments_Year, Principal)
df["Interest"] = np.ipmt(Interest_Rate/Payments_Year, df.index, Years*Payments_Year, Principal)
# df["Addl_Principal"] = -Addl_Principal
df = df.round(2)
print(df)

我的输出是:

       Payment_Date   Payment  Principal  Interest    Balance
Period                                                       
1        2019-06-01 -83333.33       0.00 -83333.33  100000.00
2        2019-07-01 -83333.33      -0.00 -83333.33  100000.00
3        2019-08-01 -83333.33       0.00 -83333.33  100000.00
4        2019-09-01 -83333.33      -0.00 -83333.33  100000.00
5        2019-10-01 -83333.33      -0.00 -83333.33  100000.00
6        2019-11-01 -83333.33      -0.00 -83333.33  100000.00
7        2019-12-01 -83333.33       0.00 -83333.33  100000.00
8        2020-01-01 -83333.33      -0.00 -83333.33  100000.00
9        2020-02-01 -83333.33      -0.00 -83333.33  100000.00
10       2020-03-01 -83333.33      -0.00 -83333.33  100000.00
11       2020-04-01 -83333.33       0.00 -83333.33  100000.00
12       2020-05-01 -83333.33       0.00 -83333.33  100000.00
13       2020-06-01 -83333.33       0.00 -83333.33  100000.00
14       2020-07-01 -83333.33      -0.00 -83333.33  100000.00
15       2020-08-01 -83333.33      -0.00 -83333.33  100000.00
16       2020-09-01 -83333.33       0.00 -83333.33  100000.00
17       2020-10-01 -83333.33      -0.00 -83333.33  100000.00
18       2020-11-01 -83333.33       0.00 -83333.33  100000.00
19       2020-12-01 -83333.33       0.00 -83333.33  100000.00
20       2021-01-01 -83333.33       0.00 -83333.33  100000.00
21       2021-02-01 -83333.33       0.00 -83333.33  100000.00
22       2021-03-01 -83333.33       0.00 -83333.33  100000.00
23       2021-04-01 -83333.33      -0.00 -83333.33  100000.00
24       2021-05-01 -83333.33      -0.00 -83333.33  100000.00
25       2021-06-01 -83333.33      -0.00 -83333.33  100000.00
26       2021-07-01 -83333.33      -0.00 -83333.33  100000.00
27       2021-08-01 -83333.33      -0.00 -83333.33  100000.00
28       2021-09-01 -83333.33       0.00 -83333.33  100000.00
29       2021-10-01 -83333.33      -0.00 -83333.33  100000.00
30       2021-11-01 -83333.33       0.00 -83333.33  100000.00
31       2021-12-01 -83333.33       0.00 -83333.33  100000.00
32       2022-01-01 -83333.33       0.00 -83333.33  100000.00
33       2022-02-01 -83333.33      -0.00 -83333.33  100000.00
34       2022-03-01 -83333.33      -0.01 -83333.33   99999.99
35       2022-04-01 -83333.33       0.00 -83333.33   99999.99
36       2022-05-01 -83333.33       0.00 -83333.33   99999.99
37       2022-06-01 -83333.33       0.00 -83333.33   99999.99
38       2022-07-01 -83333.33       0.00 -83333.33   99999.99
39       2022-08-01 -83333.33      -0.10 -83333.23   99999.89
40       2022-09-01 -83333.33      -0.21 -83333.12   99999.68
41       2022-10-01 -83333.33      -0.42 -83332.92   99999.26
42       2022-11-01 -83333.33      -0.83 -83332.50   99998.43
43       2022-12-01 -83333.33       0.00 -83333.33   99998.43
44       2023-01-01 -83333.33       0.00 -83333.33   99998.43
45       2023-02-01 -83333.33      -6.67 -83326.67   99991.76
46       2023-03-01 -83333.33      -6.67 -83326.67   99985.09
47       2023-04-01 -83333.33     -13.33 -83320.00   99971.76
48       2023-05-01 -83333.33     -26.67 -83306.67   99945.09
49       2023-06-01 -83333.33     -26.67 -83306.67   99918.42
50       2023-07-01 -83333.33     -26.67 -83306.67   99891.75
51       2023-08-01 -83333.33    -133.33 -83200.00   99758.42
52       2023-09-01 -83333.33    -133.33 -83200.00   99625.09
53       2023-10-01 -83333.33    -560.00 -82773.33   99065.09
54       2023-11-01 -83333.33    -560.00 -82773.33   98505.09
55       2023-12-01 -83333.33   -1413.33 -81920.00   97091.76
56       2024-01-01 -83333.33   -4826.67 -78506.67   92265.09
57       2024-02-01 -83333.33   -1413.33 -81920.00   90851.76
58       2024-03-01 -83333.33   -1413.33 -81920.00   89438.43
59       2024-04-01 -83333.33  -28720.00 -54613.33   60718.43
60       2024-05-01 -83333.33  -28720.00 -54613.33   31998.43

1 个答案:

答案 0 :(得分:0)

是的...最后我想了很多,我得到了输出。

代码:

Interest_Rate = 0.10
    Years = 5
    Payments_Year = 12
    Principal = 200000

    start_date = (date(2019, 5, 28))

    pmt = np.pmt(Interest_Rate / Payments_Year, Years * Payments_Year, Principal)
    per = 1

    ipmt = np.ipmt(Interest_Rate / Payments_Year, per, Years * Payments_Year, Principal)

    ppmt = np.ppmt(Interest_Rate / Payments_Year, per, Years * Payments_Year, Principal)

    rng = pd.date_range(start_date, periods=Years * Payments_Year, freq='MS')
    rng.name = "Payment_Date"

    df = pd.DataFrame(index=rng, columns=['Payment', 'Principal', 'Interest', 'Balance'],
                      dtype='float')
    df.reset_index(inplace=True)
    df.index += 1
    df.index.name = "Period"
    print(df.index)
    df["Payment"] = np.pmt(Interest_Rate / Payments_Year, Years * Payments_Year, Principal)
    df["Principal"] = np.ppmt(Interest_Rate / Payments_Year, df.index, Years * Payments_Year, Principal)
    df["Interest"] = np.ipmt(Interest_Rate / Payments_Year, df.index, Years * Payments_Year, Principal)

    df = df.round(2)

    df["Balance"] = 0
    df.loc[1, "Balance"] = Principal + df.loc[1, "Principal"]

    for i in range(2, len(df) + 1):
        prev_balance = df.ix[i - 1, 'Balance']
        principal = df.ix[i, 'Principal']

        if prev_balance == 0:
            df.ix[i, ['Payment', 'Principal', 'Interest', 'Balance']] = 0
            continue

        if abs(principal) <= prev_balance:
            df.ix[i, 'Balance'] = principal + prev_balance
        else:

            if prev_balance <= abs(principal):
                principal = -prev_balance
                addl_principal = 0
            else:
                print('else')
            df.ix[i, 'Balance'] = 0
            df.ix[i, 'Principal'] = principal
            df.ix[i, "Payment"] = principal + df.ix[i, "Interest"]

    df = df.round(2)
    print(df)