Basic functions of working with tables¶
In this example, let's look at working with tables using the DataFrames.jl
library in the Engee environment.
Table creation¶
Let's define a table and on its basis consider working with functions.
One of the ways to set a table is to get it from an existing matrix.
x = rand(3,4) # Задаем матрицу 3х4
# Подключим библиотеку для работы
using DataFrames
y = DataFrame( x, ["A", "B", "C", "D"] )
You can name the columns automatically by specifying the keyword :auto
as the second parameter.
y1 = DataFrame( x, :auto )
Functions for working with strings and columns¶
To output, for example, the second column of a table, you can refer to it by the column name dot, or you can output all rows of the second column. These two methods are represented in the code cells below.
y.B # Обращение через название столбца
y[:,2] # Выводим все строки 2-го столбца
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 argument is the new column names.
rename!(y1, ["A", "B", "C", "D"])
Column values can be sorted using the function sort()
.
sort!(y1.C) # Отсортировали значения в столбце С и записали в исходную таблицу результат
You can add a column to the table by referring to it by name and assigning a value to it. If there is no column with this name in the table, a new one will be created.
y1.F = 3 * (y1.B + y1.C) # Запишем в новый столбец результат суммы значений столбцов А и В, уможенной на 3
y1
Thus, we have considered the main basic functions of working with tables using the library DataFrames.jl
. More similar information on working with tables can be found in DataFrames.jl.