[英]Find all instances where a pandas dataframe containing a column of int's matches a dataframe with a column of a list of ints
I have 2 dataframes.我有 2 个数据框。 One (a) has a column of integers and one (b) has a column of a list of integers (array or list)
一个 (a) 有一列整数,一个 (b) 有一列整数列表(数组或列表)
I'm trying to find a way to find all occurrences of b where b contains a.我正在尝试找到一种方法来查找 b 包含 a 的所有 b 出现。
I hoped something like this would work我希望这样的事情会奏效
df3 = a[a['cell'].isin(b['cells'])]
But I get an empty dataframe.但我得到一个空的 dataframe。
I tried using columns 'a' and 'b' in the same dataframe.我尝试在同一个 dataframe 中使用列“a”和“b”。 Here is the filter that worked for me on an example dataframe I made.
这是在我制作的示例 dataframe 上为我工作的过滤器。
data=pd.DataFrame([[1, [1, 2, 3]], [5, [11, 12, 13]], [6, [7, 6, 10]]], columns=['a', 'b'])
mask=(pd.Series(data.index)).apply(lambda x:(data.loc[x, ['a']]).isin(data.loc[x, ['b']].values[0]))
data:数据:
data.loc[mask['a'].values]: data.loc[掩码['a'].values]:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.