我想过滤列中的值并为过滤结果分配一个值。但是我得到了不正确的结果,
在这种情况下,我想为 20 到 30 之间的值分配值 1,其余值为 0。但我为所有值分配了 1。任何想法为什么?
df = DataFrame(
Dict(
"time" => collect(1:10),
"cost" => [23,99,81,56,28,31,98,34,21,17]
)
)
df.cat = missing
df.cat = ifelse.(df.cost .> 20 .& df.cost .< 30, 1,0)
出去:最佳答案
问题是你需要在 df.cost .> 20
周围加上括号和 df.cost .< 30
部分:
julia> df = DataFrame(
Dict(
"time" => collect(1:10),
"cost" => [23,99,81,56,28,31,98,34,21,17]
)
);
julia> df.cat = ifelse.((df.cost .> 20) .& (df.cost .< 30), 1, 0);
julia> df
10×3 DataFrame
Row │ cost time cat
│ Int64 Int64 Int64
─────┼─────────────────────
1 │ 23 1 1
2 │ 99 2 0
3 │ 81 3 0
4 │ 56 4 0
5 │ 28 5 1
6 │ 31 6 0
7 │ 98 7 0
8 │ 34 8 0
9 │ 21 9 1
10 │ 17 10 0
关于dataframe - 在同一列中分配过滤值的结果不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66455900/