Subconjunto de um DataFrame

Introdução ao Polars

Liam Brannigan

Data Scientist and Polars Contributor

Selecionando linhas

rentals[0]
shape: (1, 8)
| name      | type    | price | bedrooms | doubles | singles | review | beach |
| ---       | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str       | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|-----------|---------|-------|----------|---------|---------|--------|-------|
| Seashells | Cottage | 540   | 4        | 2       | 2       | 8.7    | true  |
Introdução ao Polars

Selecionando linhas

rentals[-1]
shape: (1, 8)
| name                | type    | price | bedrooms | doubles | singles | review | beach |
| ---                 | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str                 | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|---------------------|---------|-------|----------|---------|---------|--------|-------|
| Tehidy Holiday Park | Cottage | 637   | 4        | 2       | 4       | 9.0    | false |
Introdução ao Polars

Selecionando um intervalo de linhas

rentals[1:3]
shape: (2, 8)
| name      | type    | price | bedrooms | doubles | singles | review | beach |
| ---       | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str       | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|-----------|---------|-------|----------|---------|---------|--------|-------|
| Seashells | Cottage | 540   | 4        | 2       | 2       | 8.7    | true  |
| Lake view | Cottage | 714   | 3        | 1       | 4       | 9.2    | true  |
Introdução ao Polars

Criando uma Series a partir de uma coluna do DataFrame

rentals["name"]
shape: (2,)
Series: 'name' [str]
[
    "Waves"
    "Seashells"
]
  • Uma coluna em [] gera uma Series
  • Series é útil para visualizações
Introdução ao Polars

Selecionando várias colunas com colchetes

rentals[["name", "price"]]
shape: (5, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
| Piran View  | 775   |
| Palma Villa | 1772  |
Introdução ao Polars

Selecionando linhas e colunas

rentals[:3, ["name", "price"]]
shape: (3, 2)
| name      | price |
| ---       | ---   |
| str       | i64   |
|-----------|-------|
| Waves     | 540   |
| Seashells | 540   |
| Lake view | 714   |
Introdução ao Polars

Fazendo subconjunto de colunas com .select()

rentals.select("name", "price")
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
df.select(["name", "price"])
Introdução ao Polars

Colchetes ou select?

rentals.select("name","price")
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |

.select() permite otimizações

rentals[["name","price"]]
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
Introdução ao Polars

Vamos praticar!

Introdução ao Polars

Preparing Video For Download...