带有 isin 的 pandas 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46806827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:39:36  来源:igfitidea点击:

pandas function with isin

pythonpandas

提问by Kvothe

I have a dataframe as like this:

我有一个像这样的数据框:

aa        bb  cc
[a, x, y] a   1
[b, d, z] b   2
[c, e, f] s   3
np.nan    d   4

I'm trying to create a new column like this:

我正在尝试创建一个这样的新列:

aa        bb  cc dd
[a, x, y] a   1  True
[b, d, z] b   2  True
[c, e, f] s   3  False
np.nan    d   4  False

My current solution is:

我目前的解决方案是:

def some_function(row):
    if row['bb].isin(row['aa'])==True:
        return True
    return False
df['dd'] = df.apply(lambda row: some_function(row), axis=1)

But this throws out an error ("'str' object has no attribute 'isin'", 'occurred at index 0')

但这会抛出一个错误 ("'str' object has no attribute 'isin'", 'occurred at index 0')

I suspect, because I'm missing something when it comes to checking the isin.

我怀疑,因为在检查isin.

Essentially, I need to check if the str value of bbis in column aawhich has a list in each cell.

本质上,我需要检查的 str 值bb是否aa在每个单元格中有一个列表的列中。

Any ideas on how to do this?

关于如何做到这一点的任何想法?

回答by jezrael

You need parameter infor check membership in list:

您需要参数in来检查列表中的成员资格:

df['dd'] = df.apply(lambda x: x.bb in x.aa, axis=1)
print (df)
          aa bb  cc     dd
0  [a, x, y]  a   1   True
1  [b, d, z]  b   2   True
2  [c, e, f]  s   3  False

EDIT:

编辑:

df['dd'] = df.apply(lambda x: (x.bb in x.aa) and (x.cc == 1), axis=1) 
print (df)
          aa bb  cc     dd
0  [a, x, y]  a   1   True
1  [b, d, z]  b   2  False
2  [c, e, f]  s   3  False

Or:

或者:

df['dd'] = df.apply(lambda x: x.bb in x.aa, axis=1) & (df['cc'] == 1)
print (df)
          aa bb  cc     dd
0  [a, x, y]  a   1   True
1  [b, d, z]  b   2  False
2  [c, e, f]  s   3  False

EDIT:

编辑:

df['dd'] = df.apply(lambda x: x.bb in x.aa if type(x.aa) == list else False, axis=1) 
print (df)
          aa bb  cc     dd
0  [a, x, y]  a   1   True
1  [b, d, z]  b   2   True
2  [c, e, f]  s   3  False
4        NaN  d   4  False