Boolean false filter in update selection query

Hello,

Am trying to update values in a dataframe as follows:

df[
    (
        df[!, :country] .== country 
        && !df[!, :bool]
    ),
    :value
] = value

However I receive error that non-boolean value is used in a boolean context.

Above works if I use df[!, :bool] (condition as true).

How can I perform above query using false condition in statement?

Thanks

Can you share a sample of df? That would make it easier to diagnose. I.e. a β€œminimum working example”

Is this what you are looking for:

julia> using DataFrames

julia> df = DataFrame(country = ["A", "B", "A", "A"], bool = [true, true, false, true], value = zeros(4))
4Γ—3 DataFrame
β”‚ Row β”‚ country β”‚ bool β”‚ value   β”‚
β”‚     β”‚ String  β”‚ Bool β”‚ Float64 β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1   β”‚ A       β”‚ 1    β”‚ 0.0     β”‚
β”‚ 2   β”‚ B       β”‚ 1    β”‚ 0.0     β”‚
β”‚ 3   β”‚ A       β”‚ 0    β”‚ 0.0     β”‚
β”‚ 4   β”‚ A       β”‚ 1    β”‚ 0.0     β”‚

julia> df[(df.country .== "A") .& df.bool, :value] = 1
1

julia> df
4Γ—3 DataFrame
β”‚ Row β”‚ country β”‚ bool β”‚ value   β”‚
β”‚     β”‚ String  β”‚ Bool β”‚ Float64 β”‚
β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1   β”‚ A       β”‚ 1    β”‚ 1.0     β”‚
β”‚ 2   β”‚ B       β”‚ 1    β”‚ 0.0     β”‚
β”‚ 3   β”‚ A       β”‚ 0    β”‚ 0.0     β”‚
β”‚ 4   β”‚ A       β”‚ 1    β”‚ 1.0     β”‚
1 Like

Yes this is what I am looking for. Thank you!

For the negation you can use

df[(df.country .== "A") .& (df.bool .!= true), :value] = 1
1 Like

Yes makes sense. Thanks again

2 Likes