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

标签 python pandas list

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

相关文章:

list - 如何隐藏 [] (或一般 [] )的 Monad 实例?

c# - 想知道一种在 C# 中使用 List<> 和结构来节省内存的方法

python - 如何将值附加到 bash shell 脚本中的空数组?

python - Python 中的八皇后问题

python - 2 个地理数据框之间的交集

python - 以 Float 形式表示的年份到 Datetime64

python - 如何将一个 Pandas 数据框列与日期结合,另一个与时间结合?

python - 在 keras-python 中使用 ImageDataGenerator 进行数据增强

python - list extend() 索引,不仅将列表元素插入到末尾

c - 使用指针为结构体分配内存