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) # Задаем матрицу 3х4
# Подключим библиотеку для работы
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 table 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 # Обращение через название столбца
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 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 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) # Запишем в новый столбец результат суммы значений столбцов А и В, уможенной на 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.