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:
Name | Team | Work_experience |
---|---|---|
String | String | Int64 |
Vivek | EPAT | 15 |
Viraj | Marketing | 8 |
Rohan | Sales | 7 |
Ishan | Quantra | 10 |
a | b |
---|---|
Float64 | Float64 |
0.845011 | 0.720306 |
0.647665 | 0.0409036 |
0.427267 | 0.221369 |
0.413642 | 0.374832 |
0.477994 | 0.118461 |
0.0849006 | 0.157679 |
0.0477405 | 0.845332 |
0.518909 | 0.159305 |
0.93499 | 0.259579 |
0.60034 | 0.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
name | team | work experience |
---|---|---|
String | String | Int64 |
Vivek | EPAT | 15 |
Viraj | Marketing | 8 |
Rohan | Sales | 7 |
Ishan | Quantra | 10 |
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
variable | mean | min | median | max | nmissing | eltype |
---|---|---|---|---|---|---|
Symbol | Float64 | Float64 | Float64 | Float64 | Int64 | DataType |
a | 0.499846 | 0.0477405 | 0.498452 | 0.93499 | 0 | Float64 |
b | 0.301368 | 0.0409036 | 0.190337 | 0.845332 | 0 | Float64 |
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"
name | team | work experience |
---|---|---|
String | String | Int64 |
Vivek | EPAT | 15 |
name | team |
---|---|
String | String |
Vivek | EPAT |
Viraj | Marketing |
Rohan | Sales |
Ishan | Quantra |
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 Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.