问题描述
我有一个数据框,其中包含有关电影的信息.它有一个名为genre
的列,其中包含它所属的流派列表.例如:
I have a dataframe, which contains info about movies. It has a column called genre
, which contains a list of genres it belongs to. For example:
df['genre']
## returns
0 ['comedy', 'sci-fi']
1 ['action', 'romance', 'comedy']
2 ['documentary']
3 ['crime','horror']
...
我想知道如何查询数据帧,以便它返回属于某类型的电影?
I want to know how can I query the dataframe, so it returns the movie belongs to a cerain genre?
例如,类似df['genre'].contains('comedy')
的东西可能返回0或1.
For example, something may like df['genre'].contains('comedy')
returns 0 or 1.
我知道列表,我可以做类似的事情:
I know for a list, I can do things like:
'comedy' in ['comedy', 'sci-fi']
但是,在大熊猫中,我没有找到类似的东西,我唯一知道的是df['genre'].str.contains()
,但是它不适用于列表类型.
However, in pandas, I didn't find something similar, the only thing I know is df['genre'].str.contains()
, but it didn't work for the list type.
推荐答案
您可以使用 apply
用于创建mask
,然后创建 boolean indexing
:
You can use apply
for create mask
and then boolean indexing
:
mask = df.genre.apply(lambda x: 'comedy' in x)
df1 = df[mask]
print (df1)
genre
0 [comedy, sci-fi]
1 [action, romance, comedy]
这篇关于python& pandas :如何查询列表类型列是否包含某些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!