在 Julia 中进行子集化时条件的否定

Negation of condition when subsetting in Julia

我想在 Julia 中对数据框进行子集化。我有 DataArrays.DataArray{String,1} 命名为 "brokenDf" 其中包含我想从数据帧和数据帧 "df".

中删除的序列号

我得到的最接近的是 "findin"

df[findin(df[:serial],brokenDf),:];

但我不知道如何在这之后翻转它,或者如果我们在 Julia 中有 NOT IN 命令。所以,它会像 findNOTin().

如有任何建议,我们将不胜感激。

下面应该做你想要的:

using DataFrames
df = DataFrame(A = 1:6, B = ["M", "F", "F", "M", "N", "N"]);

# Rows where B .== "M"
f1 = find(df[:, 2] .== "M");

# Rows where B is not "M"
f2 = find(df[:, 2] .!= "M");

# Rows where B is not "M" and is not "F"
f3 = reduce(&, (df[:, 2] .!= "F", df[:, 2] .!= "M"));

后者可以自动写一个函数:

# Define function
function find_is_not(x, conditions)
    temp = sum(x .!= conditions, 2);
    res  = find(temp .== length(conditions));
    return res;
end

# Rows where B is not "M" and is not "F" (with find_is_not)
f4 = find_is_not(df[:, 2], ["M" "F"]);

一个解决方案是使用 map() 并创建一个 Bool 数组来对数据帧的行进行子集化:

using DataFrames
df = DataFrame(serial = 1:6, B = ["M", "F", "F", "M", "N", "N"]);

broken = [1,2,5];

df[DataArray{Bool}(map(x -> !(x in broken), df[:serial])),:]

输出为:

3×2 DataFrames.DataFrame
│ Row │ serial │ B   │
├─────┼────────┼─────┤
│ 1   │ 3      │ "F" │
│ 2   │ 4      │ "M" │
│ 3   │ 6      │ "N" │

请注意,! 否定您的布尔条件,因此 !true == false

使用列表理解的解决方案是:

df = df[[!(i in brokenDf) for i in df.serial], :]

这将为您提供过滤后的 DataFrame,其中 df.serial 不在 brokenDf.