Column arithmetics
Kotlin DataFrame provides operators for applying simple arithmetic, logical, string, and comparison operations to DataColumn and ColumnReference values. These operations include, for example, adding a value to each cell in a column, multiplying a column by a value, and comparing elements in a column with a value.
When useful
In most transformations, these column operations are usually not the preferred approach in Kotlin DataFrame, because the library provides row-based APIs such as add, update, and map, which are usually recommended.
Also, the expr function is particularly useful in this context, as it allows you to write row expressions inside the Columns Selection DSL. In other words, expr works as an adapter between a column selector and a row expression.
For example,
is equivalent to
but in the first case, status is used as a DataColumn of String values, and in the second case, status is treated as a String.
However, column arithmetics might still be useful in some cases. For example, when building temporary plotting expressions. If distance is stored in meters, but you need to plot it in kilometers, you can convert it directly in the plotting expression without having to create a temporary column in your dataframe just for plotting.
Not
Negates Boolean values in a DataColumn (ColumnReference). Returns a result of the same type.
Nullable Boolean columns preserve null values.
Plus
Adding a value to a column and concatenation of a column with a String are supported. In all cases, a DataColumn (ColumnReference) is returned.
Column plus value or value plus column
Adds the value to each element of the column. The value can appear on either side of the operator.
null values are not changed by this operation.
Column plus String
Converts each element of the column to a String and concatenates it with the value.
null values are converted to the string "null".
Minus
Column minus value
Subtracts the value from each element of the column.
null values are not changed by this operation.
Value minus column
Subtracts each element of the column from the value and returns a DataColumn (ColumnReference) with the results of the subtractions.
null values from the original column remain null values in the resulting column.
Unary minus
Flips the sign of each element in the column.
null values are not changed by this operation.
Times
Multiplies each element of the column by the value.
null values are not changed by this operation.
Div
Division by zero follows Kotlin semantics of the underlying type.
Divide column by value
Divides each element of the column by the value.
Divide value by column
Divides the value by each element of the column and returns a DataColumn (ColumnReference) with the results of the divisions.
If an element of the column is null, the corresponding value in the resulting column is also null.
Compare
eq, neq, gt, and lt are available for DataColumn. Each of them returns a DataColumn of Boolean values.
eq
Compares each element of a DataColumn with the value for equality using the == operator.
neq
Compares each element of a DataColumn with the value for inequality using the != operator.
gt
Compares each element of a DataColumn with the value using the > operator.
lt
Compares each element of a DataColumn with the value using the < operator.