How to check if a dataframe column exists

Hi,

how to properly check if a column is present in a dataframe ?
E.g. verify col1, col3.

using DataFrames

df = DataFrame(col1 = [1, 2], col2 = [2, 1])

"col1" in names(df)

See Functions · DataFrames.jl

2 Likes

or use the columnindex function.

2 Likes

Neat !
Thanks everyone.

julia> df = DataFrame(randn(10, 10_000), :auto);

julia> @time hasproperty(df, :x1)
  0.000035 seconds
true

julia> @time "x1" in names(df)
  0.000434 seconds (10.00 k allocations: 330.438 KiB)
true
2 Likes

Surprised to learn that hasproperty(df, "x1") also works using a string.