# filter

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

Keeps only the rows of a [DataFrame](dataframe.html) that satisfy a [row condition](datarow.html#row-conditions). The order of the kept rows doesn't change.

Returns a new [DataFrame](dataframe.html) with the rows that satisfy the condition. If no row satisfies it, the result is an empty `DataFrame` with the same columns.

```TEXT
df.filter { DataRow.(DataRow) -> Boolean }
```

The row condition receives each row both as `this` and as `it`, so the values of the row can be accessed directly using [Extension Properties](extensionpropertiesapi.html) or [String column accessors](stringapi.html#string-column-accessors), including the values of nested columns of a [ColumnGroup](datacolumn.html#columngroup).

See also:

* [drop](drop.html), which removes the rows that satisfy a [row condition](datarow.html#row-conditions).

* [distinct](distinct.html), which removes duplicate rows.

* [takeWhile](slicerows.html#takewhile) / [dropWhile](slicerows.html#dropwhile), which keep or remove only the first rows that satisfy a condition.

* [Filter rows](filterrows.html), an overview of the operations that filter rows.

API reference: [filter](https://kotlin.github.io/dataframe/api-docs/core/org.jetbrains.kotlinx.dataframe.api/filter.html)

## Parameters

* `predicate: DataRow.(DataRow) -> Boolean` — a [row condition](datarow.html#row-conditions) that returns `true` if the row should be kept in the result.

## Examples

The following dataframe will be used in the examples below. The rows that satisfy the condition from the next example are highlighted:

```KOTLIN
df
```

[Embedded content](./resources/filterDf.html)

Keep only the people older than 18 whose first name starts with "A":

Properties:

```KOTLIN
df.filter { age > 18 && name.firstName.startsWith("A") }
```

Strings:

```KOTLIN
df.filter { "age"<Int>() > 18 && "name"["firstName"]<String>().startsWith("A") }
```

[Embedded content](./resources/filter.html)

The row can also be taken as an explicit lambda parameter instead of `it`.

Besides the row values, the row condition can use the [DataRow API](datarow.html#row-functions) — for example, the index of the row and the previous row. Keep the people starting from the third row whose previous person is happy:

```KOTLIN
df.filter { row -> row.index() >= 2 && row.prev()!!.isHappy }
```

[Embedded content](./resources/filterWithRowLambda.html)

##  `filter` on `DataColumn` 

Keeps only the values of a [DataColumn](datacolumn.html) that satisfy the given predicate.

Returns a new [DataColumn](datacolumn.html) with the same name, containing only the matching values in their original order.

```TEXT
col.filter { value -> Boolean }
```

### Parameters

* `predicate: (value) -> Boolean` — a lambda that receives a value of the column and returns `true` if the value should be kept in the result.

### Examples

Keep only the ages greater than 17:

```KOTLIN
df.age.filter { it > 17 }
```

[Embedded content](./resources/filterColumn.html)

