I imported a xlsx file to an array a using XLSX.jl. But now I have no real names for the fields in df and the types are all Any.
I changed names using rename! but I donβt know how to change types respectively to String, Int64 and Float64. Is it possible to import a vector of element types?
Is there a better method to go from xlsx to a DataFrame importing the row heading and types?
using DataFrames
# using an array
a = ["a" 2 4.2; "b" 5 6.7; "c" 3 8.9]
df = DataFrame(a)
# How to change names and types of DataFrame?
vec_of_names = ["name", "2004", "2020"]
rename!(df, vec_of_your_new_names)
You should learn the difference between vectors and matrices. You should be working with vectors here, [:name, :y2004, "2020"], note, the commas instead of spaces.
Itβs not clear what your line is trying to accomplish.
I mean something like the following:
julia> df = DataFrame(x = Any[rand() for i in 1:10], y = Union{Float64, Missing}[rand() for i in 1:10]);
julia> function narrow_types!(df)
for c in names(df)
df[!, c] = identity.(df[!, c])
end
return df
end
narrow_types! (generic function with 2 methods)
julia> narrow_types!(df)
10Γ2 DataFrame
β Row β x β y β
β β Float64 β Float64 β
βββββββΌββββββββββββΌββββββββββββ€
β 1 β 0.234185 β 0.0427855 β
β 2 β 0.519789 β 0.123238 β
β 3 β 0.838223 β 0.414821 β
β 4 β 0.436736 β 0.0125951 β
β 5 β 0.981439 β 0.867722 β
β 6 β 0.0135416 β 0.547298 β
β 7 β 0.9429 β 0.966585 β
β 8 β 0.333247 β 0.379245 β
β 9 β 0.567072 β 0.208785 β
β 10 β 0.114606 β 0.256162 β
For future reference, XLSX.readtable also has a infer_eltypes kwarg that should give back properly typed columns for your DataFrame: API Reference Β· XLSX.jl