如何使用字符串列表过滤pandas DataFrame [英] How to filter pandas DataFrame with a list of strings

查看:363
本文介绍了如何使用字符串列表过滤pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过仅将包含mylist = ['cat', 'mouse']中包含的几个字符串之一的行保留在特定列df['Title']中来过滤pandas DataFrame df:

I am trying to filter a pandas DataFrame df by keeping only the rows which contain one of several strings I have in a mylist = ['cat', 'mouse'], in a specific column df['Title']:

  df.head()

                     Title               Duration   ...       
    0             The Cat1 & Mouse2       33 min    ...          
    1             Legend of the cat       10 min    ...        
    2             Foo-Bar                 3 min     ...      
    3             Legend of Mousopia      5 min     ...          
    4             Cat + Mouse             7 min     ...    

看着类似的问题,我试图通过执行以下操作来过滤df:

Looking at similar questions, I have attempted to filter df by doing the following:

z = df['Title'].str.lower()

df = df[z.contains([x for x in mylist])]

期望df.head()看起来像这样:

                     Title               Duration   ...       
    0             The Cat1 & Mouse2       33 min    ...          
    1             Legend of the cat       10 min    ...                 
    4             Cat + Mouse             7 min     ...    

但是,我不断收到以下错误:

However, I keep getting the following error:

AttributeError: 'Series' object has no attribute 'contains'

我已经更新了condapandas,但仍然得到相同的结果.

I have updated conda and pandas but keep getting the same result.

conda version : 4.5.4
conda-build version : 3.8.0
python version : 3.6.5.final.0
pandas version : 0.23.0           py36h830ac7b_0

我想念什么?

推荐答案

尝试使用df['Title'].str.contains(*my_list).

这篇关于如何使用字符串列表过滤pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆