【问题标题】:filter pandas where some columns contain any of the words in a list过滤熊猫,其中某些列包含列表中的任何单词
【发布时间】:2020-07-24 07:13:08
【问题描述】:

我想过滤一个数据框。生成的数据框应包含在任意列中包含列表中任何单词的所有行。

我开始使用 for 循环,但应该有更好的 pythonic/pandonic 方式。

例子:

# importing pandas 
import pandas as pd 

# Creating the dataframe with dict of lists 
df = pd.DataFrame({'Name': ['Geeks', 'Peter', 'James', 'Jack', 'Lisa'], 
                   'Team': ['Boston', 'Boston', 'Boston', 'Chele', 'Barse'], 
                   'Position': ['PG', 'PG', 'UG', 'PG', 'UG'], 
                   'Number': [3, 4, 7, 11, 5], 
                   'Age': [33, 25, 34, 35, 28], 
                   'Height': ['6-2', '6-4', '5-9', '6-1', '5-8'], 
                   'Weight': [89, 79, 113, 78, 84], 
                   'College': ['MIT', 'MIT', 'MIT', 'Stanford', 'Stanford'], 
                   'Salary': [99999, 99994, 89999, 78889, 87779]}, 
                   index =['ind1', 'ind2', 'ind3', 'ind4', 'ind5']) 


df1 = df[df['Team'].str.contains("Boston") | df['College'].str.contains('MIT')] 
print(df1) 

因此很清楚如何单独过滤包含特定单词的列

进一步说明如何过滤包含列表中任何字符串的每列行:

df[df.Name.str.contains('|'.join(search_values ))]

其中 search_values 包含单词或字符串列表。

search_values = ['boston','mike','whatever']

我正在寻找一种简短的编码方式

#pseudocode
give me a subframe of df where any of the columns 'Name','Position','Team' contains any of the words in search_values

我知道我能做到

df[df['Name'].str.contains('|'.join(search_values )) | df['Position'].str.contains('|'.join(search_values )) | df['Team'].contains('|'.join(search_values )) ]

但如果我想要 20 列,那将是一堆乱七八糟的代码

有什么建议吗?

编辑奖励: 在查看列列表时,即“名称”、“职位”、“团队”如何包含索引?传递 ['index','Name','Position','Team'] 不起作用。

谢谢。

我看过这个: https://www.geeksforgeeks.org/get-all-rows-in-a-pandas-dataframe-containing-given-substring/

https://kanoki.org/2019/03/27/pandas-select-rows-by-condition-and-string-operations/

Filter out rows based on list of strings in Pandas

【问题讨论】:

    标签: python pandas filter


    【解决方案1】:

    您也可以在level=0 上使用any stack:

    cols_list = ['Name','Team'] #add column names
    df[df[cols_list].stack().str.contains('|'.join(search_values),case=False,na=False)
       .any(level=0)]
    

            Name    Team Position  Number  Age Height  Weight College  Salary
    ind1  Geeks  Boston       PG       3   33    6-2      89     MIT   99999
    ind2  Peter  Boston       PG       4   25    6-4      79     MIT   99994
    ind3  James  Boston       UG       7   34    5-9     113     MIT   89999
    

    【讨论】:

    • 嗨@anky,如果我不仅需要查询特定的列列表而且还需要查询索引,有什么建议
    • 只需重置索引并将索引名称包含在 col_list 中。默认索引被命名为'index'
    【解决方案2】:

    将apply 与any 联系起来

    df[[c1,c2..]].apply(lambda x : x.str.contains('|'.join(search_values )),axis=1).any(axis=1)
    

    【讨论】:

    • 谢谢大家。我希望能够将所有内容标记为答案。非常酷。一个班轮!!!!!!
    【解决方案3】:

    在这种情况下,您可以简单地apply:

    cols_to_filter = ['Name', 'Position', 'Team']
    search_values = ['word1', 'word2']
    
    patt = '|'.join(search_values)
    
    mask = df[cols_to_filter].apply(lambda x: x.str.contains(patt)).any(1)
    
    df[mask]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-10
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2019-03-07
      • 1970-01-01
      相关资源
      最近更新 更多