Sottoinsiemi di un DataFrame

Introduzione a Polars

Liam Brannigan

Data Scientist and Polars Contributor

Selezionare righe

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  |
Introduzione a Polars

Selezionare righe

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 |
Introduzione a Polars

Selezionare un intervallo di righe

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  |
Introduzione a Polars

Creare una Series da una colonna del DataFrame

rentals["name"]
shape: (2,)
Series: 'name' [str]
[
    "Waves"
    "Seashells"
]
  • Singola colonna in [] restituisce una Series
  • Series utile per le visualizzazioni
Introduzione a Polars

Selezionare più colonne con le parentesi

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

Selezionare righe e colonne

rentals[:3, ["name", "price"]]
shape: (3, 2)
| name      | price |
| ---       | ---   |
| str       | i64   |
|-----------|-------|
| Waves     | 540   |
| Seashells | 540   |
| Lake view | 714   |
Introduzione a Polars

Sottoinsiemi di colonne con .select()

rentals.select("name", "price")
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
df.select(["name", "price"])
Introduzione a Polars

Brackets o select?

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

.select() consente ottimizzazioni

rentals[["name","price"]]
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
Introduzione a Polars

Passons à la pratique !

Introduzione a Polars

Preparing Video For Download...