python- pandas减去列中的列中的列值的值
具有带有列的DataFrame Mortgage_data名称Mortgage_amount和一个月(以迫切顺序)
输入=
Mortgage_amount_paid = 1000
MORTGAGE_AMOUNT_PAID_DATE = 30-12-2019
Mortgage_data:
name mortgage_amount month to_be_paid_date
mark 500 1 01-01-2020
mark 500 2 01-02-2020
mark 500 3 01-03-2020
mark 500 4 01-04-2020
mark 500 5 01-05-2020
mark 500 6 01-06-2020
mark 500 7 01-07-2020
如何扣除和更新 Mortgage_Amount
,并使用 Mortgage_amount_paid
在数据框中
并添加一列 pay_status
为是,如果MERTGAGE_AMONT_PAID已完全扣除该金额,如果不喜欢的话,
如果 Mortgage_Amount_Paid = 2000
and code> Mortgage_amount_paid_date_date = 30-12-2019-2019
mortgage_data:
name mortgage_amount month to_be_paid_date mortgage_amount_updated paid_status to_be_paid_date_updated
mark 500 1 01-01-2020 0 full 30-12-2019
mark 500 2 01-02-2020 0 full 30-12-2019
mark 500 3 01-03-2020 0 full 30-12-2019
mark 500 4 01-04-2020 0 full 30-12-2019
mark 500 5 01-05-2020 500 zero 01-01-2020
mark 500 6 01-06-2020 500 zero 01-02-2020
mark 500 7 01-07-2020 500 zero 01-03-2020
ex:
if mortgage_amount_paid = 1800
and mortgage_amount_paid_date = 30-12-2019
mortgage_data:
name mortgage_amount month to_be_paid_date mortgage_amount_updated paid_status to_be_paid_date_updated
mark 600 1 01-01-2020 0 full 30-12-2019
mark 600 2 01-02-2020 0 full 30-12-2019
mark 600 3 01-03-2020 0 full 30-12-2019
mark 600 4 01-04-2020 600 zero 01-01-2020
mark 600 5 01-05-2020 600 zero 01-02-2020
mark 600 6 01-06-2020 600 zero 01-03-2020
mark 600 7 01-07-2020 600 zero 01-04-2020
this code will update till the mortgage_amount_updated
and pay_status
def new(mortgage_amount_paid, df):
m = df.mortgage_amount.cumsum()
n = mortgage_amount_paid
df['paid_status'] = np.where(m < n, 'full',
np.where(m - n < df.mortgage_amount, 'partial', 'zero'))
return df # You do not have to since it does inplace replacement
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我会将您的日期列和
to_be_paid_date
转换为dateTime,这样我就会用
iterrows()
在每一行上迭代,然后将to_be_be_paid_date
与Mortgage_amount_paid_date
并从Mortgage_amount_paid
中扣除您的Mortgage_Amount
值,直到它达到零为止。First I would convert your date column and
to_be_paid_date
to datetime like soThen I would iterate over each row with
iterrows()
and compare theto_be_paid_date
withmortgage_amount_paid_date
and deduct yourmortgage_amount
value in the row frommortgage_amount_paid
until it hits zero.