Data Manipulation and Visualization Techniques in Julia – Part II

Contributor:
QuantInsti
Visit: QuantInsti

Learn how to create new dataframes with Part I.

Basic mathematical operations

As discussed in my previous post, basic arithmetic operations can be performed on individual columns.

``````# Subtraction
df_2.a - df_2.b``````

subtraction.jl hosted with ❤ by GitHub

``````10-element Vector{Float64}:

-0.5474996670806442
0.5174063588946236
-0.564150142575268
0.12873854328766576
0.2741519215981265
0.20241852864291987
0.09324017568958975
-0.41716724316286524
0.2693306887583933
-0.5967498723478988``````

You’ll have to use the “.” operator for element-wise division.

``df_2.["a"] ./ df_2["b"] ``

elementwise operation.jl hosted with ❤ by GitHub

``````10-element Vector{Float64}:

0.06754620232737023
3.013387340201863
0.4169119702423886
1.2293455286486041
1.4462537614868343
8.482279426917298
1.1103752688515762
0.21238611891693882
3.1244976300403002
0.38733760512833965``````

Basic operations

Rearranging columns

r” is a regex search string. Here, any column with a string “work” will be selected and moved to the first place. You can write the full column name as well.

``````## Method to rearrange columns in a dataframe
select!(df_1, r"work", :)``````

rearranging columns.jl hosted with ❤ by GitHub

Adding a new column in a dataframe

Here we add another column, “c”, to the dataframe df_2.

``````df_2.c = rand(10)
df_2``````

adding new column.jl hosted with ❤ by GitHub

Dataframe-to-matrix conversion

``Matrix(df_2)``

dataframe to matrix.jl hosted with ❤ by GitHub

``````10×3 Matrix{Float64}:

0.0396604  0.58716    0.741712
0.774389   0.256983   0.429361
0.403371   0.967521   0.989583
0.690069   0.56133    0.50599
0.888493   0.614341   0.152574
0.229472   0.0270531  0.932589
0.937996   0.844756   0.0745573
0.112492   0.52966    0.712178
0.396105   0.126774   0.397762
0.377277   0.974027   0.685073``````