How to Make Empty DataFrame in julia
Overview
Despite many languages supporting dataframes, surprisingly, creating empty arrays can be a new and annoying task each time.
Code
Type Specification
julia> using DataFrames
julia> df1 = DataFrame(x = Int64[], y = String[])
0×2 DataFrame
You actually just need to insert an empty array as data. At this point, a type is specified, but when there’s absolutely no data, neither column names nor types are visible.
julia> push!(df1, [3, "three"])
1×2 DataFrame
│ Row │ x │ y │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 3 │ three │
julia> push!(df1, [3.14, "pi"])
┌ Error: Error adding value to column :x.
└ @ DataFrames C:\Users\rmsms\.julia\packages\DataFrames\GtZ1l\src\dataframe\dataframe.jl:1606
ERROR: InexactError: Int64(3.14)
Once data is inserted, column names and types are displayed correctly. Be careful, as data will not be added if the type does not match.
Without Type Specification
julia> df2 = DataFrame(x = [], y = String[])
0×2 DataFrame
julia> push!(df2, [3, "three"])
1×2 DataFrame
│ Row │ x │ y │
│ │ Any │ String │
├─────┼─────┼────────┤
│ 1 │ 3 │ three │
julia> push!(df2, [3.14, "pi"])
2×2 DataFrame
│ Row │ x │ y │
│ │ Any │ String │
├─────┼──────┼────────┤
│ 1 │ 3 │ three │
│ 2 │ 3.14 │ pi │
If you don’t want to stress about the type of the dataframe, you can simply create an empty array of Any
as shown above. Unlike when specifying types, you can see that the data is well inserted.
Environment
- OS: Windows
- julia: v1.6.2