How to replace 'Nothing' data type in an array

Looking at

curated_df[!, :sub] = [isnothing(x) ? "N/A" : x["sub"] for x in nest1_array]

The left hand side of the = is the same as what you have already used, so hopefully you understand it already, it is just a way of assigning values to a full column of the Dataframe.

The right hand side is what is called an “array comprehension”, it is a way of applying a function to each member of an array, so this would be equivalent to

function f(x)
    return isnothing(x) ? "N/A" : x["sub"]
end

curated_df[!, :sub] = [f(x) for x in nest1_array]

The ? : combination is an operator known from C and many other languages, it is just a shorthand for:

function f(x)
    if isnothing(x)
        return "N/A"
    else
        return x["sub"]
    end
end

curated_df[!, :sub] = [f(x) for x in nest1_array]

Unlike some other languages however, Julia requires whitespace around both the ? and the :

1 Like