使用
Series.str.extract
具有
re.I
对于忽略案件
fillna
以下内容:
最后添加
numpy.where
对于仅通过布尔掩码设置值:
import re
country_list= ['USA','MEXICO','Europe']
pattern = '|'.join(country_list)
mask = df['Region'] == 'Unknown'
s = (df['Name'].str.extract('(' + pattern + ')', flags=re.I, expand=False)
.fillna('Unknown'))
df['New_Region'] = np.where(mask, s, df['Region'])
print (df)
Region Name New_Region
0 Europe Project-Europe Europe
1 Unknown Project_Mexico Mexico
2 Unknown Project USA USA
3 Unknown Project Unknown
4 Paraguay Project Paraguay