筛选并按数据框列中的标签数组分组 [英] filter and group by array of tags in column of a dataframe
本文介绍了筛选并按数据框列中的标签数组分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的示例中,如何仅保留列tags
中存在的数组中具有"a"
的行?
In the following example, how do I keep only rows that have "a"
in the array present in column tags
?
df = pd.DataFrame(columns=["val", "tags"], data=[[5,["a","b","c"]]])
df[3<df.val] # this works
df["a" in df.tags] # is there an equivalent for filtering on tags?
推荐答案
我认为使用集合很直观.然后,您可以使用>=
作为设置包含项
I think using sets is intuitive. Then you can use >=
as set containment
df[df.tags.apply(set) >= {'a'}]
val tags
0 5 [a, b, c]
一个笨拙的选择是
A Numpy alternative would be
tags = df['tags']
n = len(tags)
out = np.zeros(n, np.bool8)
i = np.arange(n).repeat(tags.str.len())
np.logical_or.at(out, i, np.concatenate(tags) == 'a')
df[out]
每个 @JonClements
您可以在map
(非常聪明)中使用set.issubset
You can use set.issubset
in a map
(very clever)
df[df.tags.map({'a'}.issubset)]
val tags
0 5 [a, b, c]
这篇关于筛选并按数据框列中的标签数组分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文