Check if DataFrame names contain the names in another array

Viewed 56

I want to check if my DataFrame contains all of the columns I specified. Of course I can do it with the code below, but I have the feeling it should be possible in one line.

using DataFrames
bools = Array{Bool}([])
df = DataFrame(A=[1,2], B=[3,4], C=[5,6])
for name in ["A", "B"]
    push!(bools, name ∈ names(df))
end
false ∉ bools
1 Answers

I found it. The operator can be used to check if one Array is a subset of another:

# "⊆" can be typed by \subseteq<tab>
julia> ["A", "B"] ⊆ names(df)
true

julia> issubset(["A", "B"], names(df))
true

potentially useful as well are (not a subset), (subset but not equal) or the (super set) variants of these operators.

# "⊈" can be typed by \nsubseteq<tab>
julia> ["A", "D"] ⊈ names(df)
true

# "⊊" can be typed by \subsetneq<tab>
julia> ["A", "B", "C"] ⊊ names(df)
false

julia> ["A", "B"] ⊊ names(df)
true

# "⊇" can be typed by \supseteq<tab>
julia> ["A", "B", "C", "D"] ⊇ names(df)
true
Related