# select-columns

## Description

<mark style="color:purple;">`select-columns`</mark> *adaptor selects a list of columns from a datatable in a specified order.*

Columns can be selected based on a list of columns, text string matching, or a regular expression. These methods can be combined, so a list of columns can be supplied in addition to a regular expression to select additional columns.&#x20;

## Inputs

**`data`**\
Type: `datatable`\
Required: Yes\
The datatable containing columns to be selected.

**`column names`**\
Type: `list`\
Required: No\
The list of columns to be included in the specified order.

**`pattern`**\
Type: `text`\
Required: No\
Specifies the pattern (a text or a regular expression) to select columns whose name matches that pattern. Matching columns will be added after any columns specified in `column names`, and in the order in which they appear in the original datatable. The pattern is treated as a regular expression if it begins and ends with `/` (e.g. /.\*/).

## Outputs

**`data`**\
Type: `datatable`\
A datatable with the selected columns.

## Examples

### Example 1: Default behaviour.

Inputs:

* `data`:

  <table><thead><tr><th width="98.33391915641477">Year</th><th width="88">Month</th><th width="129">Sales (USD)</th><th width="122">Sales (GBP)</th><th width="127">Sales (EUR)</th></tr></thead><tbody><tr><td>2020</td><td>Jan</td><td>1458</td><td>1156</td><td>1345</td></tr><tr><td>2020</td><td>Feb</td><td>9874</td><td>7835</td><td>9110</td></tr><tr><td>2020</td><td>Mar</td><td>4562</td><td>3601</td><td>4173</td></tr><tr><td>2020</td><td>Apr</td><td>3654</td><td>2885</td><td>3342</td></tr></tbody></table>
* `column names`:
  1. `Month`
  2. `Sales (GBP)`

#### Outputs:

* `data`:

  <table><thead><tr><th width="109.3274336283186">Month</th><th width="146">Sales (GBP)</th></tr></thead><tbody><tr><td>Jan</td><td>1156</td></tr><tr><td>Feb</td><td>7835</td></tr><tr><td>Mar</td><td>3601</td></tr><tr><td>Apr</td><td>2885</td></tr></tbody></table>

### Example 2: Selecting columns using a regular expression.

In this example, we use the regular expression `/^Sales .*/` as a value for the `pattern` input to select columns which start with `Sales`.

Inputs:

* `data`:

  <table><thead><tr><th width="98.04973357015986">Year</th><th width="88">Month</th><th width="129">Sales (USD)</th><th width="122">Sales (GBP)</th><th width="127">Sales (EUR)</th></tr></thead><tbody><tr><td>2020</td><td>Jan</td><td>1458</td><td>1156</td><td>1345</td></tr><tr><td>2020</td><td>Feb</td><td>9874</td><td>7835</td><td>9110</td></tr><tr><td>2020</td><td>Mar</td><td>4562</td><td>3601</td><td>4173</td></tr><tr><td>2020</td><td>Apr</td><td>3654</td><td>2885</td><td>3342</td></tr></tbody></table>
* `column names`:
  1. `Month`
* `pattern`:\
  `/Sales .*/`

#### Outputs:

* `data`:

  <table><thead><tr><th width="116.05882352941177">Month</th><th width="129">Sales (USD)</th><th width="122">Sales (GBP)</th><th width="127">Sales (EUR)</th></tr></thead><tbody><tr><td>Jan</td><td>1458</td><td>1156</td><td>1345</td></tr><tr><td>Feb</td><td>9874</td><td>7835</td><td>9110</td></tr><tr><td>Mar</td><td>4562</td><td>3601</td><td>4173</td></tr><tr><td>Apr</td><td>3654</td><td>2885</td><td>3342</td></tr></tbody></table>

## Use Cases

* Remove duplicate columns after joining multiple datatables.
* Reorder columns before exporting.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cgps.gitbook.io/data-flo/reference-guide/select-columns.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
