Data Manipulation and Visualization Techniques in Julia – Part I

QuantInsti

Contributor:
QuantInsti
Visit: QuantInsti

In this article, we’ll look at data manipulation and visualization techniques in Julia. However, I’ll not get into the details of each parameter of every function, as the objective of this series is to use Julia as a tool to achieve our goal, i.e. building and backtesting trading strategies. So, we’ll stay focused on that.

You can refer to the detailed documentation of a function if you need it to solve any particular challenge you face while programming.

This article is divided into the following sections:

  • Data manipulation
    • Creating new dataframes
    • Indexing and summarising data
    • Basic mathematical operations
    • General operations
    • Grouping data
    • Dealing with missing data
  • Importing and exporting data as CSV and excel files
  • Data visualization
    • Line plot
    • Attributes of a plot
    • Scatter plot
    • Heatmap
    • Histogram
    • Pie chart
    • Plotting mathematical functions
    • Saving plots
    • Animated plots
    • Various packages for plotting in Julia

In my previous posts in this Julia programming series, I introduced the language and started with the basic syntax of Julia programming. You can check that out as well.


Data manipulation

You need to understand the data structures dealing with large heterogeneous data sets whenever you work with any programming language. In the Julia world, they are called dataframes.

Julia’s DataFrames.jl package provides a way to structure and manipulate data.

It can be installed using the “Pkg” module.

Creating new dataframes

Here’s an example of creating a new dataframe.

## Creating a new dataframe
Using Pkg
Pkg.add(“DataFrames”)
Using DataFrames
df_1 = DataFrame(Name = ["Vivek","Viraj","Rohan","Ishan"], 
               Team = ["EPAT", "Marketing", "Sales", "Quantra"],
               Work_experience = [15, 8, 7, 10]
               )

## Creating another dataframe

df_2 = DataFrame(a=rand(10), b=rand(10))

Creating a new dataframe.jl hosted with ❤ by GitHub

Output:

NameTeamWork_experience
StringStringInt64
VivekEPAT15
VirajMarketing8
RohanSales7
IshanQuantra10
ab
Float64Float64
0.8450110.720306
0.6476650.0409036
0.4272670.221369
0.4136420.374832
0.4779940.118461
0.08490060.157679
0.04774050.845332
0.5189090.159305
0.934990.259579
0.600340.115911

Column names can be accessed using the names() function.

# Accessing column 

namesnames(df_1) #As symbol

propertynames(df_1) # As names

accessing columns.jl hosted with ❤ by GitHub

Output:

3-element Vector{String}:
"Name"
"Team"
"Work_experience"

3-element Vector{Symbol}:
:Name
:Team
:Work_experience

Renaming columns can be done using the rename() function.

rename!(df_1, ["name", "team", "work experience"])

rename columns.jl hosted with ❤ by GitHub

nameteamwork experience
StringStringInt64
VivekEPAT15
VirajMarketing8
RohanSales7
IshanQuantra10

Indexing and summarising data

Indexing dataframes to use particular rows or columns for manipulation is a fundamental operation, and summarising data helps us understand it better. In Julia, summary stats of any dataset can be printed using the describe() function.

describe(df_2)

describe data.jl hosted with ❤ by GitHub

variablemeanminmedianmaxnmissingeltype
SymbolFloat64Float64Float64Float64Int64DataType
a0.4998460.04774050.4984520.934990Float64
b0.3013680.04090360.1903370.8453320Float64

Another way to find the number of rows and columns in a dataframe is using ncol() and nrow() functions.

## No. of columns and rows in a dataframe
ncol(df)
nrow(df)

row and columns.jl hosted with ❤ by GitHub

Output:
2
10

Let’s look at multiple methods of accessing rows and columns of a dataframe.

# Methods to access columns and rows in a dataframe

df_1.name

df_1."team"

df_1[1:3, "team"]

df_1[1, :]

df_1[!, 1:2]

methods of accessing columns and rows.jl hosted with ❤ by GitHub

Output:
4-element Vector{String}:
"Vivek"
"Viraj"
"Rohan"
"Ishan"

4-element Vector{String}:
"EPAT"
"Marketing"
"Sales"
"Quantra"

3-element Vector{String}:
"EPAT"
"Marketing"
"Sales"
nameteamwork experience
StringStringInt64
VivekEPAT15
nameteam
StringString
VivekEPAT
VirajMarketing
RohanSales
IshanQuantra

Stay tuned for the next installment in which Anshul Tayal will discuss basic mathematical operations.

Visit QuantInsti to read the full article: https://blog.quantinsti.com/data-manipulation-visualization-using-julia/.

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with permission from QuantInsti. The views expressed in this material are solely those of the author and/or QuantInsti and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.