pandas 覆盖列数据基础多重条件

jxct1oxe  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(138)

现有数据框架:

Id      last_dt_of_payment      Group     payer_status
A1            22/08/2022          x            1
A2            21/05/2022          x            1
A3            01/09/2022          y            1
A4            22/01/2022          y            1
A5            26/02/2022          p            1
A6            30/09/2022          s            1

预期的 Dataframe :

Id      last_dt_of_payment      Group     payer_status
A1            22/08/2022          x            1
A2            21/05/2022          x            0
A3            01/09/2022          y            1
A4            22/01/2022          y            0
A5            26/02/2022          p            1
A6            30/09/2022          s            1

我正在尝试改写付款的最后日期付款人状态基准。如果付款人状态为x或yand,则付款的最后日期是在过去3个月内,付款人状态将标记为1,否则标记为0
坚持应用逻辑来检查最近三个月的付款。

rkkpypqq

rkkpypqq1#

编辑:

groups = ['x','y']

#convert to datetimes
df['last_dt_of_payment'] = pd.to_datetime(df['last_dt_of_payment'], dayfirst=True)

#create testing Period
td = pd.Period('2022-09', freq='m')
#get column to months periods
per = df['last_dt_of_payment'].dt.to_period('m')

#chain both mask
m = df['Group'].isin(groups) & per.lt(td - 3)

#set 0
df.loc[m, 'payer_status'] = 0

print (df)
   Id last_dt_of_payment Group  payer_status
0  A1         2022-08-22     x             1
1  A2         2022-05-21     x             0
2  A3         2022-09-01     y             1
3  A4         2022-01-22     y             0
4  A5         2022-02-26     p             1
5  A6         2022-09-30     s             1

相关问题