我想选择满足多行多个条件的数据帧子集。我知道我可以按顺序执行此操作 - 首先选择与第一个条件匹配的子集,然后选择与第二个条件匹配的部分,等等,但似乎应该能够在一个步骤中完成。以下内容似乎应该有效,但事实并非如此。显然,在其他语言的 DataFrame 实现中,它确实是这样工作的。有什么想法吗?
using DataFrames
df = DataFrame()
df[:A]=[ 1, 3, 4, 7, 9]
df[:B]=[ "a", "c", "c", "D", "c"]
df[(df[:A].<5)&&(df[:B].=="c"),:]
type: non-boolean (DataArray{Bool,1}) used in boolean context
while loading In[18], in expression starting on line 5
最佳答案
这是 Julia 的事情,而不是 DataFrame 的事情:您需要 &
而不是 &&
。例如:
julia> [true, true] && [false, true]
ERROR: TypeError: non-boolean (Array{Bool,1}) used in boolean context
julia> [true, true] & [false, true]
2-element Array{Bool,1}:
false
true
julia> df[(df[:A].<5)&(df[:B].=="c"),:]
2x2 DataFrames.DataFrame
| Row | A | B |
|-----|---|-----|
| 1 | 3 | "c" |
| 2 | 4 | "c" |
FWIW,这与 Python 中的 pandas 的工作方式相同:
>>> df[(df.A < 5) & (df.B == "c")]
A B
1 3 c
2 4 c
关于dataframe - 使用多个条件选择数据框行的子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421092/