如何在pandas中的类中创建组值?

时间:2015-07-31 14:32:32

标签: python pandas dataframe

我有一个带有列的pandas数据框:

pb['Progress']

的类型是对象和值:

Choose Product
Account Access
Credit Profile
Ineligible
Declined (Credit | Internal)
Approved (Underwritten)
Declined (ID & Bank)
E-Sign (Accepted)
Declined (Underwritten)
Unauthorised
Pre-Declined
Initial Payment
E-Sign (Referred)
Approved (Automatic)
Referred (Automatic | Expired)

我想再向df添加一列

Approved (Underwritten)
Approved (Automatic)
E-Sign (Accepted)
Initial Payment

将获得标签1,其余标签为0

1 个答案:

答案 0 :(得分:0)

pb['label'] = pb['Progress']

pb['label'] = pb['label'].replace('Approved (Underwritten)', '1')
pb['label'] = pb['label'].replace('Approved (Automatic)', '1')
pb['label'] = pb['label'].replace('E-Sign (Accepted)', '1')
pb['label'] = pb['label'].replace('Initial Payment', '1')

pb['label'] = pb['label'].replace('Choose Product', '0')
pb['label'] = pb['label'].replace('Account Access', '0')
pb['label'] = pb['label'].replace('Credit Profile', '0')
pb['label'] = pb['label'].replace('Ineligible', '0')
pb['label'] = pb['label'].replace('Declined (Credit | Internal)', '0')
pb['label'] = pb['label'].replace('Declined (ID & Bank)', '0')
pb['label'] = pb['label'].replace('Declined (Underwritten)', '0')
pb['label'] = pb['label'].replace('Unauthorised', '0')
pb['label'] = pb['label'].replace('Pre-Declined', '0')
pb['label'] = pb['label'].replace('E-Sign (Referred)', '0')
pb['label'] = pb['label'].replace('Referred (Automatic | Expired)', '0')