Hello, all. I’m trying to create a DataFrame from an array of arrays that is returned from an API, and I’m having a heck of a time getting this coded! The goal is to not have to hard-code the column names, as these may change depending on what parameters are used when calling the API.
The array that I am trying to convert to a DataFrame looks like this:
78-element Array{Any,1}:
Any["Emp", "year", "quarter", "sex", "agegrp", "ownercode", "firmsize", "seasonadj", "industry", "state", "county"]
Any["3410", "2017", "3", "0", "A00", "A05", "0", "U", "00", "40", "001"]
Any["915", "2017", "3", "0", "A00", "A05", "0", "U", "00", "40", "003"]
⋮
Any["23884", "2017", "3", "0", "A00", "A05", "0", "U", "00", "40", "131"]
Any["5099", "2017", "3", "0", "A00", "A05", "0", "U", "00", "40", "133"]
If I hard-code the column names, I can create the DataFrame like this:
df = DataFrame(Emp=String[], year=String[], quarter=String[], sex=String[], agegrp=String[], ownercode=String[], firmsize=String[], seasonadj=String[], industry=String[], state=String[], county=String[])
for i = 2 : length(employment_data)
push!(df.Emp, employment_data[i][1])
push!(df.year, employment_data[i][2])
push!(df.quarter, employment_data[i][3])
push!(df.sex, employment_data[i][4])
push!(df.agegrp, employment_data[i][5])
push!(df.ownercode, employment_data[i][6])
push!(df.firmsize, employment_data[i][7])
push!(df.seasonadj, employment_data[i][8])
push!(df.industry, employment_data[i][9])
push!(df.state, employment_data[i][10])
push!(df.county, employment_data[i][11])
end
Aside from being ridiculously verbose, the column names here can’t be changed. It seems like there should be an easy way to loop through the first array (as it contains the column names), create a DataFrame with these column names, and then push the rest of the arrays to the DataFrame, but I cannot achieve a working solution.