Basic functions of working with tables
In this example, let's look at working with tables using the library. DataFrames.jl in the Engee environment.
Creating a table
Let's define a table and use it to consider working with functions.
One of the ways to set a task is to get it from an existing matrix.
x = rand(3,4) # We set the 3x4 matrix
# Connecting the library to work
using DataFrames
y = DataFrame( x, ["A", "B", "C", "D"] )
You can set column names automatically by specifying the keyword in the second parameter. :auto.
y1 = DataFrame( x, :auto )
Functions for working with rows and columns
To display, for example, the second column of the table, you can access it with a dot by the column name or display all the rows of the second column. These two methods are shown in the cells with the code below.
y.B # Accessing through the column name
y[:,2] # Output all rows of the 2nd column
You can also output all but one column, for example С.
select(y[:,:], Not("C"))
To rename columns, there is a function rename(). The first argument of the function is the table, and the second is the new column names.
rename!(y1, ["A", "B", "C", "D"])
Column values can be sorted using the function sort().
sort!(y1.C) # We sorted the values in column C and recorded the result in the source table.
You can add a column to a table by referring to it by name and assigning a value. If there is no column with that name in the table, a new one will be created.
y1.F = 3 * (y1.B + y1.C) # Let's write in a new column the result of the sum of the values of columns A and B, multiplied by 3
y1
Thus, we have reviewed the basic basic functions of working with tables using the library. DataFrames.jl. For more information on working with tables, see DataFrames.jl.