How to coerce types from data loaded via ExcelFiles.jl

Hi, I am loading a sheet from an excel file using ExcelFiles.jl. I wish to convert the types for some of the columns that are loaded as a dataframe. Some of the columns load as a float, and I wish to coerce them to an int. I have prior knowledge that these columns will always not have any decimal digits, so I am not worried about type mismatches. I have pasted a screenshot of the excel file structure. The second row sets out the columns i would like to coerce. I attempted using the code below, but this appears to have no effect. Any assistance greatly appreciated.

Capture

using ExcelFiles, DataFrames
data1 = "C:/Users/fbillimoria/OneDrive - Nexus365/DPhil/Scripts/Powermodel/data/file.xlsx"
df = (DataFrame(load(data1,"info")))
df_lab = DataFrame(load(data1,"info",nrows=2))
df_data = DataFrame(load(data1,"info",skipstartrows=2))

for i in names(df_lab)
    if df_lab[i][1] == "int"
        df_data[i] = convert(Int64,df_data[i])
    end
end

What is not clear from your question: Is your code (to convert) running or not?

If not, maybe try this line instead
df_data[!,i] = convert(Vector{Int64},df_data[!,i])

1 Like