Sorting and summarizing a DataFrame

Introduction to Polars

Liam Brannigan

Data Scientist and Polars Contributor

Sorting

rentals.sort("price")

                                                ↓

shape: (5, 8)
| name           | type    | price | bedrooms | doubles | singles | review | beach |
| ---            | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str            | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|----------------|---------|-------|----------|---------|---------|--------|-------|
| Porth Retreat  | Cottage | 249   | 3        | 1       | 4       | null   | true  |
| Newquay Resort | Cottage | 315   | 3        | 1       | 4       | 7.30   | true  |
| Perran View    | Cottage | 341   | 3        | 1       | 4       | 8.20   | false |
| Porth Caravan  | Caravan | 380   | 3        | 1       | 4       | 7.20   | true  |

                                                ↑

Introduction to Polars

Sorting in descending order

rentals.sort("price", descending=True)
shape: (5, 8)
| name           | type    | price | bedrooms | doubles | singles | review | beach |
| ---            | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str            | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|----------------|---------|-------|----------|---------|---------|--------|-------|
| Tregenna House | Cottage | 2411  | 4        | 1       | 2       | 8.70   | true  |
| Tregenna House | Cottage | 2182  | 4        | 1       | 2       | 8.70   | true  |
| Tregenna House | Cottage | 1997  | 4        | 1       | 2       | 8.70   | true  |
| Palma Villa    | Cottage | 1947  | 4        | 1       | 2       | 9.60   | true  |
| Palma Villa    | Cottage | 1772  | 4        | 1       | 2       | 9.60   | true  |
Introduction to Polars

Sorting by multiple columns

rentals.sort("bedrooms","price")

                                                           ↓

shape: (5, 8)
| name           | type    | price | bedrooms | doubles | singles | review | beach |
| ---            | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str            | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|----------------|---------|-------|----------|---------|---------|--------|-------|
| Piran View     | null    | 775   | null     | 1       | 3       | 9.60   | false |
| Porth Retreat  | Cottage | 249   | 3        | 1       | 4       | null   | true  |
| Newquay Resort | Cottage | 315   | 3        | 1       | 4       | 7.30   | true  |
| Perran View    | Cottage | 341   | 3        | 1       | 4       | 8.20   | false |
| Porth Caravan  | Caravan | 380   | 3        | 1       | 4       | 7.20   | true  |

                                                           ↑

Introduction to Polars

Finding extreme values

rentals.top_k(3, by="price")
shape: (3, 8)
| name           | type    | price | bedrooms | doubles | singles | review | beach |
| ---            | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str            | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|----------------|---------|-------|----------|---------|---------|--------|-------|
| Tregenna House | Cottage | 2411  | 4        | 1       | 2       | 8.70   | true  |
| Tregenna House | Cottage | 2182  | 4        | 1       | 2       | 8.70   | true  |
| Tregenna House | Cottage | 1997  | 4        | 1       | 2       | 8.70   | true  |
Introduction to Polars

Finding extreme values

rentals.bottom_k(3, by="price")
shape: (3, 8)
| name           | type    | price | bedrooms | doubles | singles | review | beach |
| ---            | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str            | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|----------------|---------|-------|----------|---------|---------|--------|-------|
| Porth Retreat  | Cottage | 249   | 3        | 1       | 4       | null   | true  |
| Newquay Resort | Cottage | 315   | 3        | 1       | 4       | 7.3    | true  |
| Perran View    | Hotel   | 341   | 3        | 1       | 4       | 8.2    | false |
Introduction to Polars
rentals.describe()
shape: (9, 9)
| statistic  | name             | type      | price   | bedrooms | doubles | singles | review | beach |
| ---        | ---              | ---       | ---     | ---      | ---     | ---     | ---    | ---   |
| str        | str              | str       | f64     | f64      | f64     | f64     | f64    | f64   |
|------------|------------------|-----------|---------|----------|---------|---------|--------|-------|
| count      | 49               | 48        | 49.00   | 48.00    | 49.00   | 45.00   | 47.00  | 49.00 |
| null_count | 0                | 1         | 0.00    | 1.00     | 0.00    | 4.00    | 2.00   | 0.00  |

| mean | null | null | 973.10 | 3.69 | 1.43 | 3.22 | 8.99 | 0.63 | | std | null | null | 499.10 | 0.62 | 0.68 | 1.04 | 0.71 | null | | min | Atlantic Cottage | Apartment | 249.00 | 3.00 | 1.00 | 2.00 | 7.20 | 0.00 | | 25% | null | null | 600.00 | 3.00 | 1.00 | 2.00 | 8.70 | null | | 50% | null | null | 934.00 | 4.00 | 1.00 | 4.00 | 9.10 | null | | 75% | null | null | 1150.00 | 4.00 | 2.00 | 4.00 | 9.60 | null | | max | Wheal Frances | Cottage | 2411.00 | 5.00 | 3.00 | 6.00 | 9.90 | 1.00 |
Introduction to Polars

Let's practice!

Introduction to Polars

Preparing Video For Download...