Reader small image

You're reading from  Julia Cookbook

Product typeBook
Published inSep 2016
Reading LevelBeginner
Publisher
ISBN-139781785882012
Edition1st Edition
Languages
Concepts
Right arrow
Authors (2):
Jalem Raj Rohit
Jalem Raj Rohit
author image
Jalem Raj Rohit

Jalem Raj Rohit is an IIT Jodhpur graduate with a keen interest in recommender systems, machine learning, and serverless and distributed systems. Raj currently works as a senior consultantdata scienceand NLP at Episource, before which he worked at Zomato and Kayako. He contributes to open source projects in Python, Go, and Julia. He also speaks at tech conferences about serverless engineering and machine learning.
Read more about Jalem Raj Rohit

View More author details
Right arrow

Handling data with CSV files


In this section, we will explain ways in which you can handle files with the Comma-separated Values (CSV) file format.

Getting ready

Install the DataFrames package, which is the Julia package for working with data arrays and dataframes. The command for adding the DataFrames packages to the catalog is as follows:

Pkg.add("DataFrames")

Make sure that all the installed packages are up-to-date: Pkg.update()

How to do it...

CSV files, as the name suggests, are files whose contents are separated by commas. CSV files can be accessed and read into the REPL process by executing the following steps:

  1. Assign a variable to the local source directory of the file:

    s = "/Users/username/dir/iris.csv"
    
  2. The readtable() command is used to read the data from the source. The data is read in the form of a Julia DataFrame:

    iris = readtable(s)
    

Data can be written to CSV files from a Julia DataFrame using the following steps:

  1. Create a data structure with some data inside it. For example, let's create a two-dimensional dataframe to view the the process of writing files of different formats better using DataFrames:

    df = DataFrame(A = 1:10, B = 11:20)
    
    • The preceding command creates a two-dimensional dataframe with columns named A and B.

  2. Now, the dataframe created in Step 1 can be exported to an external CSV file by using the following command:

    writetable("data.csv", df)
    
Previous PageNext Page
You have been reading a chapter from
Julia Cookbook
Published in: Sep 2016Publisher: ISBN-13: 9781785882012
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Jalem Raj Rohit

Jalem Raj Rohit is an IIT Jodhpur graduate with a keen interest in recommender systems, machine learning, and serverless and distributed systems. Raj currently works as a senior consultantdata scienceand NLP at Episource, before which he worked at Zomato and Kayako. He contributes to open source projects in Python, Go, and Julia. He also speaks at tech conferences about serverless engineering and machine learning.
Read more about Jalem Raj Rohit