Engee documentation
Notebook

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.

In [ ]:
x = rand(3,4) # Задаем матрицу 3х4
Out[0]:
3×4 Matrix{Float64}:
 0.926038   0.804963   0.534224    0.79081
 0.0455585  0.247521   0.00657984  0.944393
 0.560838   0.0451912  0.272253    0.225811
In [ ]:
# Подключим библиотеку для работы
using DataFrames

y = DataFrame( x, ["A", "B", "C", "D"] )
Out[0]:

3 rows × 4 columns

ABCD
Float64Float64Float64Float64
10.9260380.8049630.5342240.79081
20.04555850.2475210.006579840.944393
30.5608380.04519120.2722530.225811

You can name the columns automatically by specifying the keyword :auto as the second parameter.

In [ ]:
y1 = DataFrame( x, :auto )
Out[0]:

3 rows × 4 columns

x1x2x3x4
Float64Float64Float64Float64
10.9260380.8049630.5342240.79081
20.04555850.2475210.006579840.944393
30.5608380.04519120.2722530.225811

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.

In [ ]:
y.B # Обращение через название столбца 
Out[0]:
3-element Vector{Float64}:
 0.8049630878241182
 0.24752121054936527
 0.04519118103988562
In [ ]:
y[:,2] # Выводим все строки 2-го столбца
Out[0]:
3-element Vector{Float64}:
 0.8049630878241182
 0.24752121054936527
 0.04519118103988562

You can also output all but one column, for example С.

In [ ]:
select(y[:,:], Not("C"))
Out[0]:

3 rows × 3 columns

ABD
Float64Float64Float64
10.00.00.0
20.04555850.2475210.944393
30.5608380.04519120.225811

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.

In [ ]:
rename!(y1, ["A", "B", "C", "D"])
Out[0]:

3 rows × 4 columns

ABCD
Float64Float64Float64Float64
10.9260380.8049630.5342240.79081
20.04555850.2475210.006579840.944393
30.5608380.04519120.2722530.225811

Column values can be sorted using the function sort().

In [ ]:
sort!(y1.C) # Отсортировали значения в столбце С и записали в исходную таблицу результат
Out[0]:
3-element Vector{Float64}:
 0.006579837672216482
 0.2722532183791443
 0.534224047073746

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.

In [ ]:
y1.F = 3 * (y1.B + y1.C) # Запишем в новый столбец результат суммы значений столбцов А и В, уможенной на 3
y1
Out[0]:

3 rows × 5 columns

ABCDF
Float64Float64Float64Float64Float64
10.9260380.8049630.006579840.790812.43463
20.04555850.2475210.2722530.9443931.55932
30.5608380.04519120.5342240.2258111.73825

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.