Skip to content

Commit

Permalink
Update column-selection.qmd
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-iannone committed Dec 22, 2023
1 parent a43b2cd commit 717adc1
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions docs/get-started/column-selection.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ jupyter: python3
html-table-processing: none
---

The `columns=` argument for functions like `.tab_spanner()` and `.cols_move()` can accept a range of arguments.
In the above examples, we just passed a list of strings with the exact column names.
The `columns=` argument for methods like [`tab_spanner()`](`great_tables.GT.tab_spanner`) and [`cols_move()`](`great_tables.GT.cols_move`) can accept a range of arguments. In the previous examples, we just passed a list of strings with the exact column names. However, we can specify columns using any of the following:

However, we can specify columns using any of the following:

* A single string column name.
* An integer for the column's position.
* A list of strings or integers.
* A polars selector.
* A function that takes a string and returns True or False.
* a single string column name.
* an integer for the column's position.
* a list of strings or integers.
* a **Polars** selector.
* a function that takes a string and returns `True` or `False`.

```{python}
from great_tables import GT
Expand All @@ -26,50 +23,47 @@ gt_ex

## String and integer selectors

Use a list of strings or integers to select columns by name or position, respectively.
We can use a list of strings or integers to select columns by name or position, respectively.

```{python}
gt_ex.cols_move_to_start(["date", 1, -1])
gt_ex.cols_move_to_start(columns=["date", 1, -1])
```

Note the code above moved the following columns:

* The string `"date"` matched the column of the same name.
* The integer `1` matched the second column (similar to list indexing).
* The integer `1` matched the second column (this is similar to list indexing).
* The integer `-1` matched the last column.

Moreover, the order of the list defines the order of selected columns.
In this case, `"data"` was the first entry, so it's the very first column in the new table.
Moreover, the order of the list defines the order of selected columns. In this case, `"data"` was the first entry, so it's the very first column in the new table.

## Function selectors
## Using function selectors

A function can be used to select columns. It should take a string and returns True or False.
A function can be used to select columns. It should take a string and returns `True` or `False`.

```{python}
gt_ex.cols_move_to_start(lambda x: "c" in x)
gt_ex.cols_move_to_start(columns=lambda x: "c" in x)
```

## Polars selectors

When using a polars DataFrame, you can select columns using [polars selectors](https://pola-rs.github.io/polars/py-polars/html/reference/selectors.html).
## **Polars** selectors

The example below uses polars selectors to move all columns that start with `"c"` or `"f"` to the start of the table.
When using a **Polars** DataFrame, you can select columns using [**Polars** selectors](https://pola-rs.github.io/polars/py-polars/html/reference/selectors.html). The example below uses **Polars** selectors to move all columns that start with `"c"` or `"f"` to the start of the table.

```{python}
import polars as pl
import polars.selectors as cs
pl_df = pl.from_pandas(exibble)
GT(pl_df).cols_move_to_start(cs.starts_with("c") | cs.starts_with("f"))
GT(pl_df).cols_move_to_start(columns=cs.starts_with("c") | cs.starts_with("f"))
```

In general, selection should match the behaviors of the polars `DataFrame.select()` method.
In general, selection should match the behaviors of the **Polars** `DataFrame.select()` method.

```{python}
pl_df.select(cs.starts_with("c") | cs.starts_with("f")).columns
```

See the [Selectors page in the polars docs](https://pola-rs.github.io/polars/py-polars/html/reference/selectors.html) for more information.
See the [Selectors page in the polars docs](https://pola-rs.github.io/polars/py-polars/html/reference/selectors.html) for more information on this.


0 comments on commit 717adc1

Please sign in to comment.