首页 > 解决方案 > 熊猫根据 columnA.isin.list(columnB) 返回子集

问题描述

我有一个这样的数据框:

             userid  number  weight mask
0    17cf2504d0c7       1      1    56
1    17cf2504d0c7       2      5    56
2    17cf2504d0c7       3      3    123

我只是想返回一个子集,其中“权重”出现在数字“掩码”列表中

             userid  number  weight mask
1    17cf2504d0c7       2      5    56
2    17cf2504d0c7       3      3    123

我似乎很难使用迭代器,这似乎是对熊猫的浪费。

标签: pythonpandasst

解决方案


好吧,一种方法是将整数转换为set并使用集合减法。

mask_ = df.weight.astype(str).apply(set).sub(df['mask'].astype(str).apply(set)).str.len().eq(0)

然后

df.loc[mask_, :]


    userid          number  weight  mask
1   17cf2504d0c7    2       5       56
2   17cf2504d0c7    3       3       123

推荐阅读