Working with CSV Files in Julia

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# using pkg to install packages
using Pkg  
  
# using pkg to add csv package file to library
Pkg.add("CSV")

chevron_right


Install Packages

Now you have to Save your data into the CSV file.

Created .csv File

Reading 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# using Installed csv package for working with csv files
using CSV 
  
# reading the csv file 
CSV.read("myfile.csv")

chevron_right


Reading Data from csv file

Modifying 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

filter_none

edit
close

play_arrow

link
brightness_4
code

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 wite method
CSV.write("myfile.csv", ab)

chevron_right


Creating a new Dataframe 

Now we will overwrite the existing ‘.csv’ file Using CSV write() method.

Write to existing file

Writing 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



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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 drop command in Julia.

Julia

filter_none

edit
close

play_arrow

link
brightness_4
code

# Format
# CSV.File(filename;drop=["colulm1", "column2"....., column n])
  
# droping "RESIDENCE" column from our file (newfile.csv")
CSV.File("newfile.csv"; drop=["RESIDENCE"])

chevron_right


Drop column Residence

Querying 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)) 

chevron_right


Hence “N” number of operations can be performed on CSV file in Julia using built-in CSV packages with pre-defined  “M” number of methods with unique operations.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.