# countDistinct

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

Counts distinct rows or distinct combinations of values in selected columns.

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

When `countDistinct` is used on a [DataFrame](dataframe.html), it returns the number of distinct rows in this [DataFrame](dataframe.html).

```KOTLIN
df
```

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

```KOTLIN
df.countDistinct() // the result is 4
```

You can also specify which columns to use when counting distinct combinations of values.

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

Properties:

```KOTLIN
df.countDistinct { name.firstName and city } // the result is 3
```

Strings:

```KOTLIN
df.countDistinct { "name"["firstName"] and "city" } // the result is 3
```

When `countDistinct` is used on a `GroupBy`, it counts distinct rows within each group. That is, this function returns a [DataFrame](dataframe.html) where each row corresponds to a group from the original `GroupBy`. The result contains the original group key columns and a new column with the number of distinct rows (or combinations of values in selected columns) in each group.

Let's take this `GroupBy` as an example:

```KOTLIN
df.groupBy { city }
```

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

Applying `countDistinct` to this `GroupBy` yields the following result:

Properties:

```KOTLIN
df.groupBy { city }.countDistinct()
```

Strings:

```KOTLIN
df.groupBy("city").countDistinct()
```

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

You can also specify which columns in the groups should be used to determine distinctness.

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

Properties:

```KOTLIN
df.groupBy { city }.countDistinct { name.firstName }
```

Strings:

```KOTLIN
df.groupBy("city").countDistinct { "name"["firstName"] }
```

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

The default name of the new column is `countDistinct`, but you can choose a different one.

Properties:

```KOTLIN
df.groupBy { city }.countDistinct("uniqueFirstNames") { name.firstName }
```

Strings:

```KOTLIN
df.groupBy("city").countDistinct("uniqueFirstNames") { "name"["firstName"] }
```

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

