# Operations

> For the complete documentation index, see [llms.txt](https://kotlin.github.io/dataframe/llms.txt).

A data transformation pipeline usually consists of several modification operations, such as filtering, sorting, grouping, pivoting, adding/removing columns etc. The Kotlin DataFrame API is designed in a functional style so that the whole processing pipeline can be represented as a single statement with a sequential chain of operations. [DataFrame](dataframe.html) object is immutable and all operations return a new [DataFrame](dataframe.html) instance reusing underlying data structures as much as possible.

For full signature details and KDocs of all operations, see the [Dokka API Reference](https://kotlin.github.io/dataframe/api-docs/index.html) (or [Core API Package](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/index.html)).

```KOTLIN
df.update { age }.where { city == "Paris" }.with { it - 5 }
    .filter { isHappy && age > 100 }
    .move { name.firstName and name.lastName }.after { isHappy }
    .merge { age and weight }.by { "Age: ${it[0]}, weight: ${it[1]}" }.into("info")
    .rename { isHappy }.to("isOK")
```

> **Tip:**
> You can play with the "people" dataset that is used in this guide [here](https://datalore.jetbrains.com/view/notebook/aOTioEClQQrsZZBKeUPAQj)

## Multiplex operations

Simple operations (such as [filter](filter.html) or [select](select.html)) return new [DataFrame](dataframe.html) immediately, while more complex operations return an intermediate object that is used for further configuration of the operation. Let's call such operations multiplex.

Every multiplex operation configuration consists of:

* [column selector](columnselectors.html) that is used to select target columns for the operation

* additional configuration functions

* terminal function that returns modified [DataFrame](dataframe.html)

Most multiplex operations end with `into` or `with` function. The following naming convention is used:

* `into` defines column names for storing operation results. Used in [move](move.html), [group](group.html), [split](split.html), [merge](merge.html), [gather](gather.html), [groupBy](groupby.html), [rename](rename.html).

* `with` defines row-wise data transformation with [row expression](datarow.html#row-expressions). Used in [update](update.html), [convert](convert.html), [replace](replace.html), [pivot](pivot.html).

## List of DataRow operations

* `index(): Int` — sequential row number in [DataFrame](dataframe.html), starts from 0;

* `prev(): DataRow?` — previous row (`null` for the first row);

* `next(): DataRow?` — next row (`null` for the last row);

* `diff(T) { rowExpression }: T / diffOrNull { rowExpression }: T?` — difference between the results of a [row expression](datarow.html#row-expressions) calculated for the current and previous rows;

* `explode(columns): DataFrame<T>` — spread lists and [DataFrame](dataframe.html) objects vertically into new rows;

* `values(): List<Any?>` — list of all cell values from the current row;

* `valuesOf<T>(): List<T>` — list of values of the given type;

* `columnsCount(): Int` — number of columns;

* `count(): Int` — equivalent to `columnsCount()`;

* `count(predicate): Int` — number of values in the row satisfying the given predicate;

* `columnNames(): List<String>` — list of all column names;

* `columnTypes(): List<KType>` — list of all column types;

* `namedValues(): List<NameValuePair<Any?>>` — list of name-value pairs where `name` is a column name and `value` is a cell value;

* `namedValuesOf<T>(): List<NameValuePair<T>>` — list of name-value pairs where the value has the given type;

* `transpose(): DataFrame<NameValuePair<*>>` — [DataFrame](dataframe.html) with two columns: `name: String` for column names and `value: Any?` for cell values;

* `transposeTo<T>(): DataFrame<NameValuePair<T>>` — [DataFrame](dataframe.html) with two columns: `name: String` for column names and `value: T` for cell values;

* `getRow(Int): DataRow` — row from the [DataFrame](dataframe.html) by a row index;

* `getRows(Iterable<Int>): DataFrame` — [DataFrame](dataframe.html) with a subset of rows selected by absolute row indices;

* `relative(Iterable<Int>): DataFrame` — [DataFrame](dataframe.html) with a subset of rows selected by relative row indices: `relative(-1..1)` will return the previous, current, and next row. Requested indices will be coerced to the valid range and invalid indices will be skipped;

* `getValue<T>(columnName)` — cell value of type `T` by this row and the given `columnName`;

* `getValueOrNull<T>(columnName)` — cell value of type `T?` by this row and the given `columnName` or `null` if there's no such column;

* `get(column): T` — cell value by this row and the given `column`;

* `String.invoke<T>(): T` — cell value of type `T` by this row and the given `this` column name;

* `ColumnPath.invoke<T>(): T` — cell value of type `T` by this row and the given `this` column path;

* `ColumnReference.invoke(): T` — cell value of type `T` by this row and the given `this` column;

* `df()` — [DataFrame](dataframe.html) that the current row belongs to.

## List of DataRow statistics

The following [statistics](summarystatistics.html) are available for `DataRow`:

* `rowSum`

* `rowMean`

* `rowStd`

These statistics will be applied only to values of appropriate types, and incompatible values will be ignored. For example, if a [dataframe](dataframe.html) has columns of types `String` and `Int`, `rowSum()` will compute the sum of the `Int` values in the row and ignore `String` values.

To apply statistics only to values of a particular type, use `-Of` versions:

* `rowSumOf<T>`

* `rowMeanOf<T>`

* `rowStdOf<T>`

* `rowMinOf<T>`

* `rowMaxOf<T>`

* `rowMedianOf<T>`

* `rowPercentileOf<T>`

## List of DataFrame operations

* [add](add.html) — add columns

* [addId](add.html#addid) — add `id` column

* [append](append.html) — add rows

* [columns](columns.html) / [columnNames](columnnames.html) / [columnTypes](columntypes.html) — get list of top-level columns, column names or column types

* [columnsCount](columnscount.html) — number of top-level columns

* [concat](concat.html) — union rows from several [DataFrame](dataframe.html) objects

* [convert](convert.html) — change column values and/or column types

* [corr](corr.html) — pairwise correlation of columns

* [count](count.html) — number of rows that match condition

* [countDistinct](countdistinct.html) — number of unique rows

* [cumSum](cumsum.html) — cumulative sum of column values

* [describe](describe.html) — basic column statistics

* [distinct](distinct.html) / [distinctBy](distinct.html#distinctby) — remove duplicated rows

* [drop](drop.html) / [dropLast](slicerows.html#droplast) / [dropWhile](slicerows.html#dropwhile) / [dropNulls](drop.html#dropnulls) / [dropNA](drop.html#dropna) / [dropNaNs](drop.html#dropnans) — remove rows by condition

* [duplicate](duplicate.html) — duplicate rows

* [explode](explode.html) — spread lists and [DataFrame](dataframe.html) objects vertically into new rows

* [fillNulls](fill.html#fillnulls) / [fillNaNs](fill.html#fillnans) / [fillNA](fill.html#fillna) — replace missing values

* [filter](filter.html) — filter rows by condition

* [first](first.html) / [firstOrNull](first.html#firstornull) — find first row by condition

* [flatten](flatten.html) — remove column groupings recursively

* [forEach / forEachIndexed](iterate.html) — iterate over rows, columns, cells, the values of a column, or the key–group pairs of a `GroupBy`

* [format](format.html) — conditional formatting for cell rendering

* [gather](gather.html) — convert pairs of column names and values into new columns

* [getColumn](getcolumn.html) / [getColumnOrNull](getcolumn.html#getcolumnornull) / [getColumnGroup](getcolumn.html#getcolumngroup) / [getColumns](getcolumn.html#getcolumns) — get one or several columns

* [group](group.html) — group columns into [ColumnGroup](datacolumn.html#columngroup)

* [groupBy](groupby.html) — group rows by key columns

* [head](head.html) — get first 5 rows of [DataFrame](dataframe.html)

* [implode](implode.html) — collapse column values into lists grouping by other columns

* [inferType](infertype.html) — infer column type from column values

* [insert](insert.html) — insert column

* [join](join.html) — join two [DataFrame](dataframe.html) objects by key columns

* [joinWith](joinwith.html) — join two [DataFrame](dataframe.html) objects by an expression that evaluates joined [DataRows](datarow.html) to Boolean

* [last](last.html) / [lastOrNull](last.html#lastornull) — find last row by condition

* [map](map.html) — map columns into new [DataFrame](dataframe.html) or [DataColumn](datacolumn.html)

* [max](minmax.html) / [maxBy](minmax.html) / [maxOf](minmax.html) / [maxFor](minmax.html) — max of values

* [mean](mean.html) / [meanOf](mean.html) / [meanFor](mean.html) — average of values

* [median](median.html) / [medianOf](median.html) / [medianFor](median.html) — median of values

* [merge](merge.html) — merge several columns into one

* [min](minmax.html) / [minBy](minmax.html) / [minOf](minmax.html) / [minFor](minmax.html) — min of values

* [move](move.html) — move columns or change column groupings

* [parse](parse.html) — try to convert strings into other types

* [pivot](pivot.html) / [pivotCounts](pivot.html#pivotcounts) / [pivotMatches](pivot.html#pivotmatches) — convert values into new columns

* [remove](remove.html) — remove columns

* [rename](rename.html) — rename columns

* [reorder](reorder.html) / [reorderColumnsBy](reorder.html#reordercolumnsby) / [reorderColumnsByName](reorder.html#reordercolumnsbyname) — reorder columns

* [replace](replace.html) — replace columns

* [reverse](reverse.html) — reverse rows

* [rows](rows.html) / [rowsReversed](rows.html#rowsreversed) — get rows in direct or reversed order

* [rowsCount](rowscount.html) — number of rows

* [schema](schema.html) — schema of columns: names, types and hierarchy

* [select](select.html) — select subset of columns

* [shuffle](shuffle.html) — reorder rows randomly

* [single](single.html) / [singleOrNull](single.html#singleornull) — get single row by condition

* [sortBy](sortby.html) / [sortByDesc](sortby.html#sortbydesc) / [sortWith](sortby.html#sortwith) — sort rows

* [split](split.html) — split column values into new rows/columns or inplace into lists

* [std](std.html) / [stdOf](std.html) / [stdFor](std.html) — standard deviation of values

* [sum](sum.html) / [sumOf](sum.html) / [sumFor](sum.html) — sum of values

* [take](slicerows.html#take) / [takeLast](slicerows.html#takelast) / [takeWhile](slicerows.html#takewhile) — get first/last rows

* [toList](tolist.html) / [toListOf](tolist.html#tolistof) — export [DataFrame](dataframe.html) into a list of data classes

* [toMap](tomap.html) — export [DataFrame](dataframe.html) into a map from column names to column values

* [unfold](unfold.html) — unfold objects (normal class instances) in columns according to their properties

* [ungroup](ungroup.html) — remove column groupings

* [update](update.html) — update column values preserving column types

* [values](values.html) — [Sequence](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/-sequence/) of values traversed by row or by column

* [valueCounts](valuecounts.html) — counts for unique values

* [xs](xs.html) — slice [DataFrame](dataframe.html) by given key values

## Shortcut operations

Some operations are shortcuts for more general operations:

* [rename](rename.html), [group](group.html), [flatten](flatten.html) are special cases of [move](move.html)

* [valueCounts](valuecounts.html) is a special case of [groupBy](groupby.html)

* [pivotCounts](pivot.html#pivotcounts), [pivotMatches](pivot.html#pivotmatches) are special cases of [pivot](pivot.html)

* [fillNulls](fill.html#fillnulls), [fillNaNs](fill.html#fillnans), [fillNA](fill.html#fillna) are special cases of [update](update.html)

* [convert](convert.html) is a special case of [replace](replace.html)

You can use these shortcuts to apply the most common [DataFrame](dataframe.html) transformations easier, but you can always fall back to general operations if you need more customization.

