python- pandas减去列中的列中的列值的值

发布于 2025-02-09 19:14:24 字数 3364 浏览 1 评论 0 原文

具有带有列的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

Have a dataframe mortgage_data with columns name mortgage_amount and month (in asceding order)

input=
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

How to deduct and update mortgage_amount and shift up to_be_paid_date 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 = 2000 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     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 paid_status

Python- Pandas Subtract columns value in ascending order value of a column

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2025-02-16 19:14:24

首先,我会将您的日期列和 to_be_paid_date 转换为dateTime,这样

df["to_be_paid_date"] = pd.to_datetime(df["to_be_paid_date"], format="%d-%m-%Y")
mortgage_amount_paid_date = datetime.strptime(mortgage_amount_paid_date,"%d-%m-%Y")

我就会用 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 so

df["to_be_paid_date"] = pd.to_datetime(df["to_be_paid_date"], format="%d-%m-%Y")
mortgage_amount_paid_date = datetime.strptime(mortgage_amount_paid_date,"%d-%m-%Y")

Then I would iterate over each row with iterrows() and compare the to_be_paid_date with mortgage_amount_paid_date and deduct your mortgage_amount value in the row from mortgage_amount_paid until it hits zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文