1 回答
TA贡献1851条经验 获得超3个赞
numpy.select与列表理解一起使用,in用于检查列之间的子字符串:
m1 = df['1_Match'] == 'No'
m2 = [c.lower() in n.lower() for c, n in zip(df['Country'], df['Nation'])]
masks = [m1, m2]
vals = ['Not Applicable','Yes']
df['2_Match'] = np.select(masks, vals, default='No')
print (df)
Country Nation 1_Match 2_Match
0 EU Europe Yes Yes
1 MA MACOPEC No Not Applicable
2 APAC INDIA Yes No
编辑:
m1 = df['1_Match'] == 'No'
m2 = [c.lower() in n.lower() for c, n in zip(df['Country'], df['Nation'])]
m3 = df['1_1_Match'] == 'Yes'
masks = [m3, m1, m2]
vals = ['Yes', 'Not Applicable','Yes']
df['2_Match'] = np.select(masks, vals, default='No')
print (df)
Country Nation 1_Match 1_1_Match 2_Match
0 EU Europe Yes No Yes
1 MA MACOPEC No No Not Applicable
2 APAC INDIA Yes No No
3 COPEC MACOPEC No Yes Yes
编辑2:
masks = [m1 & ~m3, m2]
vals = ['Not Applicable','Yes']
print (df)
Country Nation 1_Match 1_1_Match 2_Match
0 EU Europe Yes No Yes
1 MA MACOPEC No No Not Applicable
2 APAC INDIA Yes No No
3 COPEC MACOPEC No Yes Yes
4 COPEC India No Yes No
添加回答
举报