根据另一列中的字符串值在pandas dataframe中填充一列



我有一个带有聊天信息的文本列,我想传递一个单词列表,如果它们出现在该文本列中,如果单词不出现,则将错误列修改为1和0:

chatid text                  card_declined booking_error website_error
401    hi my card declined..        0              0             0
402    you website crashed..        0              0             0
403    hi my card declined..        0              0             0
for example 
carddeclined = ['card declined', 'Card error']
for i in df[df['textchat'].str.contains('|'.join(carddeclined),na=False)]:
df['card declined'] = 1
This currently just returns all card declined rows with 1

您可以将转换后的布尔级数赋值为整数,因为列表中的一些大写值在Series.str.contains中添加了case=False参数:

carddeclined = ['card declined', 'Card error']
df['card_declined'] = df['text'].str.contains('|'.join(carddeclined),
na=False, 
case=False).astype(int)
print (df)
chatid                   text  card_declined  booking_error  website_error
0     401  hi my card declined..              1              0              0
1     402  you website crashed..              0              0              0
2     403  hi my card declined..              1              0              0

最新更新