logo

Reading Only Columns from a CSV File in Julia 📂Julia

Reading Only Columns from a CSV File in Julia

Guide

20211206_151035.png

Let’s say we have an example.csv file like the one above. When loading it into a dataframe, sometimes we want to create an entirely empty dataframe that only retains the column names, without importing all the data. This is necessary in cases where an empty dataframe is needed.

using CSV

# Loading the dataframe with no rows
df_empty = CSV.read("example.csv", DataFrame; limit = 0)

In the resulting dataframe created above, it has all three columns intact but hasn’t imported any of their contents.

CSV.read(limit = 1)

  • By using the limit = 1 option, the dataframe is loaded with only one line.

CSV.read()[[false],:]

  • By not referring to any rows with a bit array of length $1$ [false], an empty dataframe remained.
# Verifying column names remain intact
column_names = names(df_empty)

By checking the column names with the names() function, we can confirm that the column names are appropriately retained.

# Inserting new data into the empty dataframe
push!(df_empty, [1, "new_data", 3.14])

When inserting new data with push!(), it works properly just like the original dataframe.

What if just doing limit = 0?

# Attempting to load dataframe with limit 0 to get empty dataframe
df_empty_error = CSV.read("example.csv", DataFrame; limit = 0)

A StackOverflowError occurs.

Environment

  • OS: Windows
  • julia: v1.6.3