1

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

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"))]

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

4

1 回答 1

3

Rpython,有多个小的不同。在这里我们isinpandas

还值得一提的是,您创建的数据框位于Rnot pandas.

R : data.frame pandas : 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"))]
于 2020-07-05T01:01:23.277 回答