Issue
Have a dataframe mortgage_data with columns name mortgage_amount and month (in asceding order)
mortgage_amount_paid = 1000
mortgage_data:
name mortgage_amount month
mark 400 1
mark 500 2
mark 200 3
How to deduct and update mortgage_amount
in ascending order or month using mortgage_amount_paid
row by row in a dataframe
and add a column paid_status
as yes if mortgage_amount_paid is fully deducted for that amount and no if not like this
if mortgage_amount_paid = 1000
mortgage_data:
name mortgage_amount month mortgage_amount_updated paid_status
mark 400 1 0 full
mark 500 2 0 full
mark 200 3 100 partial
ex:
if mortgage_amount_paid = 600
mortgage_data:
name mortgage_amount month mortgage_amount_updated paid_status
mark 400 1 0 full
mark 500 2 300 partial
mark 200 3 200 zero
tried this:
mortgage_amount_paid = 600
# amount saved - debt
m1 = df['mortgage_amount'].cumsum().sub(mortgage_amount_paid)
# is it positive?
m2 = m1>0
# is the previous month also positive?
m3 = m2.shift(fill_value=False)
df['mortgage_amount_updated'] = (m1.clip(0, mortgage_amount_paid)
.mask(m3, df['mortgage_amount'])
)
df['paid_status'] = np.select([m3, m2], ['zero', 'partial'], 'full')
error: i have given mortgage_amount_paid=400. paid status should be paid,zero,zero. i'm getting paid,partial,zero
and
mortgage_amount_paid = 600
m = df['mortgage_amount'].cumsum()
df['paid_status'] = np.select(
[m <= mortgage_amount_paid,
(m > mortgage_amount_paid) & (m.shift() < mortgage_amount_paid)
],
['full', 'partial'],
default='zero'
)
df['mortgage_amount_updated'] = np.select(
[df['paid_status'].eq('full'),
df['paid_status'].eq('partial')],
[0, m-mortgage_amount_paid],
default=df['mortgage_amount']
)
error : if mortgage_amount_paid=1 paid_status should be partial,zero,zero. i'm getting partial zero,zero,zero
Solution
You can write a function:
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
new(600, df)
name mortgage_amount month paid_status
0 mark 400 1 full
1 mark 500 2 partial
2 mark 200 3 zero
new(1000, df)
name mortgage_amount month paid_status
0 mark 400 1 full
1 mark 500 2 full
2 mark 200 3 partial
new(100, df)
name mortgage_amount month paid_status
0 mark 400 1 partial
1 mark 500 2 zero
2 mark 200 3 zero
new(2000, df)
name mortgage_amount month paid_status
0 mark 400 1 full
1 mark 500 2 full
2 mark 200 3 full
Answered By - onyambu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.