【问题标题】:Python: Filter values with multiple criteria and listsPython:使用多个条件和列表过滤值
【发布时间】:2020-07-05 00:54:13
【问题描述】:

我有一个数据框,我想在其中过滤两列(框和类型)。其中一列是列表(类型)。

df = data.frame(boxes = c(1, 1, 1, 2, 3, 3),
                val  = c(1, 2, 3, 6, 7, 8),
                type = c("honey","bread","coffee","bread","honey","coffee"))

这不起作用。它没有给我匹配:

df[(df['boxes'] == 1) &
(df['type'] == ("honey","coffee"))]

我做错了什么?我试过了,&,|。有什么想法吗?

【问题讨论】:

    标签: python pandas data-manipulation


    【解决方案1】:

    Rpython ,有多个小的不同。这里我们在pandas中有isin

    另外值得一提的是,您创建的数据框位于 R 而非 pandas 下。

    R : data.framepandas : DataFrame

    df_sub=df[(df['boxes'] == 1) & (df['type'].isin(["honey","coffee"])].copy()
    

    同样在R 中使用%in% 将您的代码更改为下面

    df[(df['boxes'] == 1) & (df['type'] %in% c("honey","coffee"))]
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2013-07-21
      • 1970-01-01
      • 2016-11-16
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      相关资源
      最近更新 更多