【问题标题】:Pandas/Numpy: Using multiple conditional statements with Numpy where and transformPandas/Numpy:在 Numpy where 和 transform 中使用多个条件语句
【发布时间】:2019-07-25 08:48:34
【问题描述】:

在 Python 3 中,我正在尝试创建一个指标列,用于指示数据中的每个合约是否存在两个条件。

(1) 如果合约的所有未完成余额都 == 0,则合约无效

(2) 如果contract_maturity_date 早于最小date_report_created,则该合约无效

我掌握的数据如下:

import pandas as pd

example_data = {'contract_no': [1,1,1,2,2,2],
                'date_report_created': ['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-01', '2019-01-02', '2019-01-03'],
                'contract_maturity_date': ['2018-01-01', '2018-01-01', '2018-01-01', '2019-01-15', '2019-01-15', '2019-01-15'],
                'outstanding_balance': [0, 0, 0, 20, 0, 0]}
example_data = pd.DataFrame(example_data, columns = ['contract_no',
                                                     'date_report_created',
                                                     'contract_maturity_date',
                                                     'outstanding_balance'])

看起来像这样:

   contract_no date_report_created contract_maturity_date  outstanding_balance
0            1          2019-01-01             2018-01-01                    0
1            1          2019-01-02             2018-01-01                    0
2            1          2019-01-03             2018-01-01                    0
3            2          2019-01-01             2019-01-15                   20
4            2          2019-01-02             2019-01-15                    0
5            2          2019-01-03             2019-01-15                    0

我希望数据看起来像这样:

   contract_no date_report_created contract_maturity_date  outstanding_balance valid_contract_flag
0            1          2019-01-01             2018-01-01                    0             Invalid
1            1          2019-01-02             2018-01-01                    0             Invalid
2            1          2019-01-03             2018-01-01                    0             Invalid
3            2          2019-01-01             2019-01-15                   20               Valid
4            2          2019-01-02             2019-01-15                    0               Valid
5            2          2019-01-03             2019-01-15                    0               Valid

到目前为止,我只能满足条件 (1),我不确定如何将第二个条件添加到逻辑中。

import numpy as np
example_data['payment_information_in_database'] = np.where(example_data.groupby('contract_no')['outstanding_balance']
                                                                       .transform('sum') == 0, 'Invalid', 'Valid')

如果能在这个问题上提供任何帮助,我将不胜感激!

【问题讨论】:

    标签: r python-3.x pandas numpy pandas-groupby


    【解决方案1】:

    我认为你需要在这里申请:

    s=df.groupby('contract_no').apply(lambda x:x.contract_maturity_date.lt(x.date_report_created)
    &(x.outstanding_balance.sum()==0)).reset_index(drop=True)
    df['valid_contract_flag']=np.where(s,'Invalid','Valid')
    

    array(['Invalid', 'Invalid', 'Invalid', 'Valid', 'Valid', 'Valid'],
      dtype='<U7')
    

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 2021-05-03
      • 2021-04-08
      • 2013-04-26
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 1970-01-01
      相关资源
      最近更新 更多