概述
我有下面的代码,它从“Jobtitle”列中的一个大字符串中提取术语,并将指定的单词(在本例中为“engineer”、“scientist”、“analyst”)分配给一个名为“job_cat”的新列
问题
目前,后续每行代码都会覆盖上面的行,例如,只有行“analyst”适用,“Jobtitle”列中的所有其他值都是“other”,甚至应该是“engineer”或“scientist”
如何构造代码,以便将所有3个值提取到新列“Jobtitle”中
glassdoor['job_cat'] = np.where(glassdoor['Jobtitle'].str.contains('engineer'), 'engineer', 'other')
glassdoor['job_cat'] = np.where(glassdoor['Jobtitle'].str.contains('scientist'), 'scientist', 'other')
glassdoor['job_cat'] = np.where(glassdoor['Jobtitle'].str.contains('analyst'), 'analyst', 'other')
像这样的
输出
我看到您正在使用函数
extract
函数而不是contains。这会同时获得所有匹配项李>fillna
插入other李>输出:
选项1:使用^{} 的优雅解决方案
输出:
选项2:多次使用
np.where
相关问题 更多 >
编程相关推荐