我想检查一对选项,看看其中一个是否在另一个列表中。我可以检查每个系列是否单独在列表中,但我不知道怎么说 if x in list or y in list, then return True
t1 = pd.Series(['A','B','C'],name='t1')
t2 = pd.Series(['E','F','G'],name = 't2')
test = pd.concat([t1,t2],axis=1)
include = ['A','F']
# create new column Included as
test["Included"] =
我的第一个想法(行不通):
test['t1inc'] = test['t1'].isin(include)
test['t2inc'] = test['t1'].isin(include)
test['Included'] = ["Y" if x == True or y == True else "N" for x,y in test['t1inc'],test['t2inc']]
期望的输出:
print(test)
t1 t2 Included
0 A E True
1 B F True
2 C G False
最佳答案
您可以将 isin
与 any
一起使用:
test['included'] = test.isin(include).any(1)
print(test)
t1 t2 included
0 A E True
1 B F True
2 C G False
关于python - 检查 Pair Series 值以查看它们是否在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60174158/