【发布时间】:2019-06-18 23:04:42
【问题描述】:
处理贷款数据。 我有一个包含列的数据框:
df_irr = df1[['id', 'funded_amnt_t', 'Expect_NoPayments','installment']]
贷款ID |资助金额 |预期付款次数 |固定分期年金。
我已经通过回归分析估计了付款次数。 贷款期限为 36 或 60 个月。
现在我正在尝试计算预期的 irr(内部收益率)。
但我被卡住了
我打算使用numpy.irr
但是,我从来没有机会使用它——因为我的日期格式不正确?
我已经尝试过 pandas 的 pivot 和 reshape 函数。没有运气。
现金流的时间序列: - 列:月 0 , ...., 60 - 行:每笔贷款的 ID - 第 0 个月的值 = - funded_amount - 0-60 月的值:如果 expected_number_of_payments > 月,则分期付款
我的旧 Stata 代码是:
keep id installment funded_amnt expectednumberofpayments
sort id
expand 61, generate(expand)
bysort id : gen month = _n
gen cf = 0
replace cf = installment if (expectednumberofpayments+1)>=month
replace cf = funded_amnt*-1 if month==1
【问题讨论】:
-
你有什么问题?
-
:-) 需要用于重塑/构建数据框的代码。
-
我现在尝试的东西:df_irr2 = pd.DataFrame(data=df_irr, index= 'id', columns= 'funded_amnt_t' 'Expect_NoPayments''installment') df_irr3 = df_irr.groupby(by = df_irr ['id']) df_irrT.pivot(index='id', columns='Expect_NoPayments', values='installment')
标签: python pandas pivot reshape irr