3

Suppose I have a DataFrame with numeric elements. I want to check that all the elements are non-negative. I can do something like:

df .> 0

which results in a DataFrame of ones and zeros. How do I reduce it to a one true/false value?

1 Answers1

2

The almost non-allocating and efficient way to do it is:

all(all.(>(0), eachcol(df)))

or

all(all.(x -> isless(0, x), eachcol(df)))

depending on how you want to handle missing values.

Here is an example of the difference:

julia> df = DataFrame(a=[1, missing], b=1:2)
2×2 DataFrame
 Row │ a        b     
     │ Int64?   Int64 
─────┼────────────────
   1 │       1      1
   2 │ missing      2

julia> all(all.(>(0), eachcol(df)))
missing

julia> all(all.(x -> isless(0, x), eachcol(df)))
true

as with isless missing value is treated as greater than any other value.

Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107