How to Import a CSV File into Julia (example included)

You can use the following template in order to import a CSV file into Julia:

using CSV
CSV.read("The path where your CSV file is stored\\File Name.csv")

But just in case you were wondering, I’ll show you the full steps to import a CSV file into Julia.

Steps to Import a CSV File into Julia

Step 1: Install the CSV package

In order to install the CSV package, you’ll need to open the Julia command-line:

Julia 1.1.0

You’ll then see this display:

How to Import a CSV File into Julia (example included)

Type the following code in the command-line, and then press ENTER:

using Pkg

using pkg

Finally, to complete the installation of the CSV package, type the code below, and then press ENTER:

Pkg.add("CSV")

How to Import a CSV File into Julia

It would take about a minute for the installation to complete.

Step 2: Save your data in a CSV file

Next, save your data in a CSV file.

For demonstration purposes, let’s say that you have the following data about Cars:

BrandPriceYear
Honda Civic220002015
Toyota Corolla250002013
Ford Focus270002018
Audi A4350002018

You can copy that data into a CSV file, and then rename that file as Cars:

CSV file

Step 3: Import the CSV file into Julia

In order to import the CSV file into Julia, you’ll need to use the template that you saw at the beginning of this guide:

using CSV
CSV.read("The path where your CSV file is stored\\File Name.csv")

Here are few points to consider when importing your CSV file:

  • Use double backslash within the path name to avoid any errors when importing your file
  • At the end of the path, specify your file name + .csv. In our example, the file name is Cars, while the file type is csv. Therefore, the end of the path would be: Cars.csv

In my case, I stored the CSV file on my desktop, under this path:

C:\\Users\\Ron\\Desktop\\Cars.csv

So this is the code that I used to import the CSV file:

using CSV
CSV.read("C:\\Users\\Ron\\Desktop\\Cars.csv")

I then ran the code in Juypter notebook (before you execute the code, make the necessary adjustments to your path name):

Jupyter Julia

And this is the result:

Jupyter output example

As you can see, the output matches with the data that was stored in the CSV file.

Optionally, you can get the same results by applying the code in the Julia’s command-line:

Julia command line

You’ll then get the same data-set about Cars:

How to Import a CSV File into Julia (example included)