gpt4 book ai didi

python - 检查 Pair Series 值以查看它们是否在列表中

转载 作者:行者123 更新时间:2023-12-01 06:25:23 25 4
gpt4 key购买 nike

我想检查一对选项,看看其中一个是否在另一个列表中。我可以检查每个系列是否单独在列表中,但我不知道怎么说 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

最佳答案

您可以将 isinany 一起使用:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com