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
work experience | name | team |
---|---|---|
Int64 | String | String |
15 | Vivek | EPAT |
8 | Viraj | Marketing |
7 | Rohan | Sales |
10 | Ishan | Quantra |
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
a | b | c |
---|---|---|
Float64 | Float64 | Float64 |
0.845011 | 0.720306 | 0.962749 |
0.647665 | 0.0409036 | 0.10846 |
0.427267 | 0.221369 | 0.197592 |
0.413642 | 0.374832 | 0.967406 |
0.477994 | 0.118461 | 0.0233091 |
0.0849006 | 0.157679 | 0.936764 |
0.0477405 | 0.845332 | 0.296003 |
0.518909 | 0.159305 | 0.514714 |
0.93499 | 0.259579 | 0.620951 |
0.60034 | 0.115911 | 0.0224133 |
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
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.