简体   繁体   English

Pandas 中的布尔索引结合可变数量的列

[英]Boolean indexing in pandas combining a variable number of columns

Is there a (pythonic) way to loop over columns in dataframe for boolean-indexing in pandas ?有没有一种(pythonic)方法来循环数据帧中的列以在熊猫中进行布尔索引?

I'm not sure you can do this using list comprehension...我不确定你可以使用列表理解来做到这一点......

The only way I've found would be :我发现的唯一方法是:

df = pd.DataFrame(
    [
    ["a", "b", "c"],
    ["d", "e", "f"],
    ["g", "h", "i"],
    ], 
    columns=["A", "B", "C"],
    )

my_filter = {
    "A":{"a", "g"},
    "B":{"e"},
    }
    
series = [
        df[f].isin(v)==True for k,v in my_filter.items()
        ]
s = pd.DataFrame(series, columns=series[0].index).T.any(axis=1)
ix = s[s==True].index
df.loc[ix]

This seems to do the job... But I'm not sure this is memory efficient and (IMHO) this is neither straightforward nor easy to understand.这似乎可以完成这项工作......但我不确定这是否具有内存效率并且(恕我直言)这既不简单也不易于理解。

Is there a way in pandas to make some kind of serialisation of the "|"大熊猫有没有办法对“|”进行某种序列化or "&" operators ?或“&”运算符?

让我们试试

out = df[np.any([df[k].isin(v) for k,v in my_filter.items()], axis=0)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM