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
using DataFrames

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

Next, you’ll see the full steps to import a CSV file into Julia using a simple example.

Steps to Import a CSV File into Julia

Step 1: Install the relevant packages

You can install the CSV package using the following syntax:

using Pkg
Pkg.add("CSV")

You can then install the DataFrames package using:

using Pkg
Pkg.add("DataFrames")

Step 2: Import the CSV file into Julia

Let’s suppose that you have the following data stored in a CSV file called products:

product_id product_name price
1 Oven 800
2 Microwave 350
3 Toaster 80
4 Refrigerator 1100
5 Dishwasher 900

In order to import that CSV file into Julia, you may use the following template:

using CSV
using DataFrames

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

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 products, while the file type is .csv

For example, let’s suppose that the products file is stored under the following path:

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

Therefore, the code to import the CSV file for our example is as follows:

using CSV
using DataFrames

df = CSV.read("C:\\Users\\Ron\\Desktop\\products.csv", DataFrame)
print(df)

Once you run the code (adjusted to your path name), you’ll get the following result:

product_id product_name price
1 Oven 800
2 Microwave 350
3 Toaster 80
4 Refrigerator 1100
5 Dishwasher 900