Python Dataframe-如何检查列A中的字符串值是否在列B中的字符串项列表中可用

Python Dataframe-如何检查列A中的字符串值是否在列B中的字符串项列表中可用,python,pandas,dataframe,Python,Pandas,Dataframe,这是我的dataframe,它有两列:A列包含字符串,B列包含字符串列表 import pandas as pd df = pd.DataFrame(columns=['A','B']) df.loc[0] = ['apple',['orange','banana','blueberry']] df.loc[1] = ['orange',['orange','banana','avocado']] df.loc[2] = ['blueberry',['apple','banana','blue

这是我的dataframe,它有两列:A列包含字符串,B列包含字符串列表

import pandas as pd

df = pd.DataFrame(columns=['A','B'])
df.loc[0] = ['apple',['orange','banana','blueberry']]
df.loc[1] = ['orange',['orange','banana','avocado']]
df.loc[2] = ['blueberry',['apple','banana','blueberry']]
df.loc[3] = ['cherry',['apple','orange','banana']]

print(df)

           A                            B
0      apple  [orange, banana, blueberry]
1     orange    [orange, banana, avocado]
2  blueberry   [apple, banana, blueberry]
3     cherry      [apple, orange, banana]
我想检查每一行,看看A列中的值是否列在同一行B列中的列表中。因此,预期输出应为:

0 False
1 True
2 True
3 False
我尝试了
isin
,它可以根据静态列表进行检查:

df.A.isin(['orange','banana','blueberry'])
0    False
1     True
2    False
3    False
但是,当我尝试使用它检查dataframe中的列表项时,它不起作用:

df.A.isin(df.B)
TypeError: unhashable type: 'list'
如果有一个使用Pandas的解决方案,我想避免for-loop和lambda


非常感谢您的帮助。

最快的方法是在中通过
检查来理解纯列表:

m = pd.Series([i in j for i, j in zip(df.A, df.B)], index=x.index)
print (m)
0    False
1     True
2     True
3    False
dtype: bool
应用
的解决方案

m = df.apply(lambda x: x.A in x.B, axis=1)
print (m)
0    False
1     True
2     True
3    False
dtype: bool
谢谢,@pir for use图形计时解决方案:

from numpy.core.defchararray import equal

def jez1(x):
    return pd.Series([i in j for i, j in zip(x.A, x.B)], index=x.index)

def jez2(x):
    return x.apply(lambda x: x.A in x.B, axis=1)

def pir1(x):
    return x.A.apply(lambda x: set([x])) <= x.B.apply(set)
def pir2(x):
    return pd.DataFrame(x.B.tolist(), x.index).eq(x.A, 0).any(1)
def pir3(x):
    return x.B.str.join('|').str.get_dummies().mul(pd.get_dummies(x.A)).any(1)

def pir4(x):
    return pd.Series(equal(x.A.values.astype(str), np.array(x.B.tolist()).T).any(0),x.index)

def pir5(x):   
    i = np.arange(len(x)).repeat(x.B.str.len())
    return pd.Series(np.bincount(i, x.A.values[i] == np.concatenate(x.B)).astype(bool),x.index)

使用
设置的乐趣

Numpy广播 仅当
B
中的每个列表长度相同时才有效

from numpy.core.defchararray import equal

pd.Series(
    equal(df.A.values.astype(str), np.array(df.B.tolist()).T).any(0),
    df.index
)

0    False
1     True
2     True
3    False
dtype: bool

pd.get\u假人

np.bincount
我喜欢这个(-:
然而,耶兹雷尔注意到了糟糕的表现):所以要当心

i = np.arange(len(df)).repeat(df.B.str.len())
pd.Series(
    np.bincount(i, df.A.values[i] == np.concatenate(df.B)).astype(bool),
    df.index
)

0    False
1     True
2     True
3    False
dtype: bool

理解是我最喜欢的。添加了一个新的(-:是的。即使它们不同,它也应该有效lengths@piRSquared-是的,如果数据帧较大,则解决方案的扩展性很差。感谢@jezrael将所有具有图形计时的解决方案放在一起。这非常有用!我可以使用一些图形解决方案进行计时吗?是的,请使用(:我比较了上一个解决方案,在[69]中似乎非常慢:%%timeit…:I=np.arange(len(df)).repeat(df.B.str.len())…:B=pd.Series(…:np.bincount(I,df.A.values[I]==np.concatenate(df.B)).aType(bool),…:df.index…::1个循环,最好是[71]中每个循环3:635毫秒):%%timeit…:a=pd.Series([i在j中表示i,j在zip中表示(df.a,df.B)]):100个循环,每个循环最好3:5.1毫秒
Wow!这真是太慢了(-:是的,我也很惊讶。B列中有相同长度的列表吗?
df.A.apply(lambda x: set([x])) <= df.B.apply(set)

0    False
1     True
2     True
3    False
dtype: bool
pd.DataFrame(df.B.tolist(), df.index).eq(df.A, 0).any(1)

0    False
1     True
2     True
3    False
dtype: bool
from numpy.core.defchararray import equal

pd.Series(
    equal(df.A.values.astype(str), np.array(df.B.tolist()).T).any(0),
    df.index
)

0    False
1     True
2     True
3    False
dtype: bool
df.B.str.join('|').str.get_dummies().mul(pd.get_dummies(df.A)).any(1)

0    False
1     True
2     True
3    False
dtype: bool
i = np.arange(len(df)).repeat(df.B.str.len())
pd.Series(
    np.bincount(i, df.A.values[i] == np.concatenate(df.B)).astype(bool),
    df.index
)

0    False
1     True
2     True
3    False
dtype: bool