Working with CSV Files in Julia
Last Updated :
17 Jan, 2022
CSV file (Comma-separated values file) is a plain text file that uses commas to separate values and fields. It is majorly used to store data in the form of tables or spreadsheets. Each row of a table or spreadsheet is a record filled with data that belongs to n fields (or Columns). It is used to import or export data and tables very easily and stored with the extension ".csv" in most programming languages.
Julia provides various file handling methods to perform operations on CSV files. These methods can be used to create a CSV file, add contents to the file, Update the File, etc.
Importing a CSV File in Julia
First, you need to Install CSV Package using the following commands on the Julia command line:
using pkg
pkg.add("Package name")
CSV Package is a built-in package with a defined "N" number of methods to perform n operations.
Julia
# using pkg to install packages
using Pkg
# using pkg to add csv package file to library
Pkg.add("CSV")
Install Packages
Now you have to Save your data into the CSV file.
Created .csv FileReading Data from a CSV File
Here we will use the CSV package and read() method in order to read the contents of the CSV File:
Julia
# using Installed csv package for working with csv files
using CSV
# reading the csv file
CSV.read("myfile.csv")
Reading Data from csv fileModifying Contents of a CSV File
Here we will learn how to modify the content of an existing file with the help of the write() method in CSV package and DataFrames Package.
Julia
using CSV
# using dataframes package to create a dataframe
using DataFrames
# Creating DataFrame
ab = DataFrame(Name = ["AKANKSHA", "TANYA", "PREETIKA", "VRINDA", "JAHNVI"],
Age = [42, 44, 22, 81, 93],
Salary = [540000, 650000, 900000, 770000, 850000],
RESIDENCE=["DELHI", "DELHI", "UP", "HARYANA", "UP"]
)
# modifying the content of myfile.csv using write method
CSV.write("myfile.csv", ab)
Creating a new Dataframe
Now we will overwrite the existing '.csv' file Using CSV write() method.
Write to existing fileWriting to a CSV File
Here we will create a new file using touch() command and therefore use DataFrames and CSV packages to write the newly created dataframe content to a new file.
Julia
# new file created
touch("newfile.csv")
# file handling in write mode
efg = open("newfile.csv", "w")
# Creating a new dataframe
mn = DataFrame(Name = ["AKANKSHA", "TANYA", "PREETIKA", "VRINDA", "JAHNVI"],
Age = [42, 44, 22, 81, 93],
Salary = [540000, 650000, 900000, 770000, 850000],
RESIDENCE=["DELHI", "DELHI", "UP", "HARYANA", "UP"]
)
# writing to the newly created file
CSV.write("newfile.csv", mn)

Deleting contents of a CSV File
Here we will learn to delete a particular column entry or multiple column entries from a particular table or spreadsheet using the drop command in Julia.
Julia
# Format
# CSV.File(filename;drop=["colulm1", "column2"....., column n])
# dropping "RESIDENCE" column from our file (newfile.csv")
CSV.File("newfile.csv"; drop=["RESIDENCE"])
Drop column ResidenceQuerying a CSV File
Here we will learn to query a particular column or set of columns as per demand from the entire table or spreadsheet, and we can even use operators in the query to retrieve a set of rows satisfying the particular condition.
Julia
# Format for select command
# CSV.File(file; select=[column1, column2])
# Select the columns 'Name and Salary'
CSV.File("newfile.csv"; select=["Name", "Salary"])
# Selecting columns number wise
# selecting column 1 and 3
CSV.File("newfile.csv"; select=(i, nm) -> i in (1, 3))
# selecting column 1, 2, 3
CSV.File("newfile.csv"; select=(i, nm) -> i in (1, 2, 3))
Hence "N" number of operations can be performed on a CSV file in Julia using built-in CSV packages with a pre-defined "M" number of methods with unique operations.
while using CSV.read("myfile.csv"), if you find error like ""ArgumentError: provide a valid sink argument""use the below command to solve this error:
Julia
using DataFrames
file = CSV.read("myfile.csv",DataFrames)
Similar Reads
Working with Excel Files in Julia
Julia is a high-level open-source programming language meaning that its source is freely available. It is a language that is used to perform operations in scientific computing. Julia is used for statistical computations and data analysis. Julia provides its users with some pre-defined functions and
7 min read
Working with Text Files in Julia
Julia is Programming Language that is fast and dynamic in nature (most suitable for performing numerical and scientific applications) and it is optionally typed (the rich language of descriptive type and type declarations which is used to solidify our program) and general-purpose and open-sourced. J
4 min read
Working with Databases in Julia
There are several ways through which data handling can be performed in Julia. Julia can be connected to a lot of databases whose connectors directly connect to Database Independent Interface (DBI) packages such as MySQL, SQLite, PostgreSQL, etc. These can be used for firing queries and obtaining the
4 min read
Working with DataFrames in Julia
A Data frame is a two-dimensional data structure that resembles a table, where the columns represent variables and rows contain values for those variables. It is mutable and can hold various data types. Julia is a high performance, dynamic programming language which has a high-level syntax. The Data
7 min read
Working with Date and Time in Julia
Julia provides a library to use Dates for dealing with date and time. The Dates module comes inbuilt with the Julia we just need to import it in order to use it. Now we need to prefix every function with an explicit type Dates, e.g Dates.Date. If you donât want to prefix on each function just add us
5 min read
Storing Output on a File in Julia
Julia provides a vast library to store and save the output in multiple file formats. We can store the output in various forms such as CSV(comma-separated value) or in Excel or just simply a text file. Storing Data on Text FileUsing open() function In order to store the data in a text file, we need t
4 min read
Opening and Reading a File in Julia
File handling in Julia is achieved using functions such as open(), read(), and close(). There are many ways to read the contents of a file like readline(), readlines() and just read(). open(): To open a file existing in an absolute path, provided as the parameter.  read(): Read the contents of the f
4 min read
Strings in Julia
Strings in Julia are a set of characters or a single character that is enclosed within double quotes(" "). It is a finite sequence of characters. Julia allows extracting elements of a string to form multiple substrings with the use of square brackets([ ]). Creating a String Strings in Julia can be c
6 min read
Sorting of Arrays in Julia
The process of arranging the given data in a particular order or manner depends on the output. It is storing of data in sorted order. Sorting can be done in two ways i.e in ascending and descending order. Sorting can be performed using sorting algorithm or sorting functions. So, in sorting the progr
8 min read
Julia - Working with Matplotlib's Pyplot Class
Data visualization is the process of representing the available data diagrammatically. There are packages that can be installed to visualize the data in Julia. Here, PyPlot class of the Matplotlib Module in Julia will be used for plotting. To install the PyPlot package in Julia, the Matplotlib packa
5 min read