【问题标题】:Is there a way to filter a pandas dataframe row by arrays in the column? [duplicate]有没有办法按列中的数组过滤熊猫数据框行? [复制]
【发布时间】:2020-04-01 18:36:53
【问题描述】:

我有一个数据框,其输出如下:

     fruit                     season
0   apples      [plant, plant, plant]
1  oranges    [harvest, plant, plant]
2  bananas  [harvest, plant, harvest]

我想在季节栏搜索模式...

pattern = [harvest, plant, plant]

...所以它向我展示了一个过滤后的数据框,其给出的模式如下:

1  oranges    [harvest, plant, plant]

【问题讨论】:

    标签: python pandas dataframe filter


    【解决方案1】:

    如果我没有误解您希望显示具有该确切值的行: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

    pattern = ['harvest', 'plant', 'plant']     
    df.loc[df['season'] == pattern]
    

    【讨论】:

    • 这里不工作
    • 这也行不通。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2017-08-28
    • 2021-04-10
    相关资源
    最近更新 更多