【发布时间】:2019-11-17 14:11:20
【问题描述】:
如果 A 列有句子:hb_mumbai 炼油厂数据。所以我需要检查 A 列是否包含 hb_mumbai,然后将 B 列的值更新为:hb_mumbai
我尝试使用 contains 函数来实现它,但它没有产生所需的输出。
if df['A'].str.contains('hb_mumbai'):
df['B']=='hb_mumbai'
实际结果:B 列的值未更新 期望的结果:B 列的值应该得到更新。
【问题讨论】:
-
你可以使用
numpy.where,这样更有效,df.A = np.where(df.B.str.contains('hb_mumbai'),'hb_mumbai', df.A)。也做import numpy as np。 -
在使用 where() 时,我们可以在 contains() 函数中给出多个条件吗?
标签: python substring contains sentence