> ## Documentation Index
> Fetch the complete documentation index at: https://docs.graphext.com/llms.txt
> Use this file to discover all available pages before exploring further.

# filter_duplicates

> Filter duplicate rows, keeping the first or last of each set of duplicates found only. 

## Usage

The following example shows how the step can be used in a recipe.

<Accordion title="Examples" icon="code" defaultOpen="true">
  <Tabs>
    <Tab title="Example 1">
      To keep only the first row amongst a set of duplicates, identifying duplicates by inspecting
      values in columns "address" and "name"

      ```stan theme={null}
      filter_duplicates(ds, {"columns": ["address", "name"], "keep": "first"}) -> (ds_filtered)
      ```
    </Tab>

    <Tab title="Signature">
      General syntax for using the step in a recipe. Shows the inputs and outputs the step is expected to receive and will produce respectively. For futher details see sections below.

      ```stan theme={null}
      filter_duplicates(ds_in: dataset, {
          "param": value,
          ...
      }) -> (ds_out: dataset)
      ```
    </Tab>
  </Tabs>
</Accordion>

## Inputs & Outputs

The following are the inputs expected by the step and the outputs it produces. These are generally
columns (`ds.first_name`), datasets (`ds` or `ds[["first_name", "last_name"]]`) or models (referenced
by name e.g. `"churn-clf"`).

<Accordion title="Inputs" icon="right-to-bracket">
  <ParamField path="ds_in" type="dataset" required>
    An input dataset to filter.
  </ParamField>
</Accordion>

<Accordion title="Outputs" icon="right-from-bracket">
  <ParamField path="ds_out" type="dataset" required>
    A dataset containing the same columns as the input dataset but including or excluding the matched rows.
  </ParamField>
</Accordion>

## Configuration

The following parameters can be used to configure the behaviour of the step by including them in
a json object as the last "input" to the step, i.e. `step(..., {"param": "value", ...}) -> (output)`.

<Accordion title="Parameters" defaultOpen="true" icon="sliders">
  <ParamField path="columns" type="[array[string], null]">
    Names of columns used to detect and filter rows containing duplicate values.
    If not provided, will inspect all columns. Note that multivalued columns, i.e. those containing lists
    of values will always be ignored when searching for duplicates (but will be included in the result).

    <Accordion title="Array items">
      <ParamField path="Item" type="string (ds_in.column)">
        Each item in array.
      </ParamField>
    </Accordion>
  </ParamField>

  <ParamField path="keep" type="string" default="first">
    Which of a duplicate set of rows to keep in the result.
    Specifically, whether to keep the first or last row amongst the duplicates.

    Values must be one of the following:

    * `first`
    * `last`
  </ParamField>

  <ParamField path="exclude" type="boolean" default="false">
    if `true`, inverts the row selection.
    I.e., only rows being duplicates (in the selected columns) will be included in the resulting dataset.
  </ParamField>

  <ParamField path="presort" type="object">
    Row sorting before de-duplication.
    E.g. when the order of first or last duplicate to retain depends on other variables. If not configured,
    no sorting will be performed.

    <Accordion title="Properties">
      <ParamField path="by" type="[null, string, array]">
        Sort column name(s).
        These column(s) will be used to sort the dataset before de-duplication (if multiple, in specified order).

        <Accordion title="Options">
          <Tabs>
            <Tab title="null">
              <ParamField path="{_}" type="null">
                null.
              </ParamField>
            </Tab>

            <Tab title="string">
              <ParamField path="{_}" type="string (ds_in.column)">
                string.
              </ParamField>
            </Tab>

            <Tab title="array">
              <ParamField path="{_}" type="array[string]">
                array.

                <Accordion title="Array items">
                  <ParamField path="Item" type="string (ds_in.column)">
                    Each item in array.
                  </ParamField>
                </Accordion>
              </ParamField>
            </Tab>
          </Tabs>
        </Accordion>
      </ParamField>

      <ParamField path="ascending" type="[boolean, array[boolean]]" default="true">
        Whether to sort in ascending order (or in descending order if false).
        If an array, must have the same length as `by` and specify the sort order for each column.

        <Accordion title="Array items">
          <ParamField path="Item" type="boolean">
            Each item in array.
          </ParamField>
        </Accordion>
      </ParamField>
    </Accordion>
  </ParamField>
</Accordion>
