Вибірка з DataFrame

Вступ до Polars

Liam Brannigan

Data Scientist and Polars Contributor

Вибір рядків

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  |
Вступ до Polars

Вибір рядків

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 |
Вступ до Polars

Вибір діапазону рядків

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  |
Вступ до Polars

Створення Series зі стовпця DataFrame

rentals["name"]
shape: (2,)
Series: 'name' [str]
[
    "Waves"
    "Seashells"
]
  • Один стовпець у [] повертає Series
  • Series корисна для візуалізацій
Вступ до Polars

Вибір кількох стовпців за дужками

rentals[["name", "price"]]
shape: (5, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
| Piran View  | 775   |
| Palma Villa | 1772  |
Вступ до Polars

Вибір рядків і стовпців

rentals[:3, ["name", "price"]]
shape: (3, 2)
| name      | price |
| ---       | ---   |
| str       | i64   |
|-----------|-------|
| Waves     | 540   |
| Seashells | 540   |
| Lake view | 714   |
Вступ до Polars

Вибір підмножини стовпців за допомогою .select()

rentals.select("name", "price")
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
df.select(["name", "price"])
Вступ до Polars

Дужки чи select?

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

.select() дає змогу робити оптимізації

rentals[["name","price"]]
shape: (3, 2)
| name        | price |
| ---         | ---   |
| str         | i64   |
|-------------|-------|
| Waves       | 540   |
| Seashells   | 540   |
| Lake view   | 714   |
Вступ до Polars

Давайте потренуємось!

Вступ до Polars

Preparing Video For Download...