Polars DataFrame 入門

Polars 入門

Liam Brannigan

Data Scientist and Polars Contributor

認識你的講師

$$

  • Liam Brannigan,首席資料科學家
  • ML 與資料工程專家
  • Polars 貢獻者

講師的大頭照。

Polars 入門

表格資料

shape: (49, 8)
| name        | type    | price | bedrooms | doubles | singles | review | beach |
| ---         | ---     | ---   | ---      | ---     | ---     | ---    | ---   |
| str         | str     | i64   | i64      | i64     | i64     | f64    | bool  |
|-------------|---------|-------|----------|---------|---------|--------|-------|
| Waves       | Cottage | 540   | 4        | 1       | 2       | 8.9    | false |
| Seashells   | Cottage | 540   | 4        | 2       | 2       | 8.7    | true  |
| Lake view   | Cottage | 714   | 3        | 1       | 4       | 9.2    | true  |
| Piran View  | null    | 775   | null     | 1       | 3       | 9.6    | false |
| Palma Villa | Cottage | 1772  | 4        | 1       | 2       | 9.6    | true  |
Polars 入門

並排的北極熊在雪地行走與熊貓在森林中的圖片。

  • Polars 通常比 Pandas 更快 → 平行運算
  • Polars 的 lazy mode 可最佳化查詢
Polars 入門

Apache Arrow 與 Rust 的標誌。

  • Apache Arrow:在記憶體中儲存表格資料
  • Rust:用於資料處理的高速語言
Polars 入門

用資料庫示意圖來說明載入資料。

  • 載入並檢視資料
Polars 入門

用資料庫與齒輪示意圖來說明轉換。

  • 載入並檢視資料
  • 轉換資料與最佳化
Polars 入門

用資料庫、齒輪與圖表示意圖來說明分析。

  • 載入並檢視資料
  • 轉換資料與最佳化
  • 資料分析
Polars 入門

讀取 CSV

import polars as pl


rentals = pl.read_csv("vacation_rentals.csv")
Polars 入門

DataFrame 的前幾列

rentals.head(3)

rentals DataFrame 的前幾列。

Polars 入門

DataFrame 的前幾列

rentals.head(3)

標示出形狀的 rentals DataFrame。

Polars 入門

DataFrame 的前幾列

rentals.head(3)

標示出第一欄的 rentals DataFrame。

  • dtype:欄位中的資料型別
Polars 入門

DataFrame 的前幾列

rentals.head(3)

標示出 rentals 欄的 rentals DataFrame。

Polars 入門

DataFrame 的最後幾列

rentals.tail()
shape: (5, 8)
| name                | type      | price | bedrooms | doubles | singles | review | beach |
| ---                 | ---       | ---   | ---      | ---     | ---     | ---    | ---   |
| str                 | str       | i64   | i64      | i64     | i64     | f64    | bool  |
|---------------------|-----------|-------|----------|---------|---------|--------|-------|
| Hengar Manor House  | Cottage   | 615   | 4        | 1       | 5       | 9.4    | false |
| Tudor Cottage Hayle | Cottage   | 489   | 3        | 2       | 3       | 8.9    | false |
| Tolcarne Apartments | Apartment | 1764  | 3        | 1       | 4       | 9.3    | true  |
| Tolcarne Apartments | Apartment | 1605  | 3        | 1       | 4       | 9.3    | true  |
| Tehidy Holiday Park | Cottage   | 637   | 4        | 2       | 4       | 9.0    | false |
Polars 入門

DataFrame 中繼資料

rentals.shape
(49, 8)
rentals.columns
['name', 'type', 'price', 'bedrooms', 'doubles', 'singles', 'review', 'beach']
Polars 入門

DataFrame 結構綱要

rentals.schema
Schema([('name', String),
        ('type', String),
        ('price', Int64),
        ('bedrooms', Int64),
        ('doubles', Int64),
        ('singles', Int64),
        ('review', Float64),
        ('beach', Boolean)])
Polars 入門

檢視 DataFrame

rentals.glimpse()
Rows: 49
Columns: 8
$ name      <str> 'Waves', 'Seashells', 'Lake view', 'Piran View',...
$ type      <str> 'Cottage', 'Cottage', 'Cottage', None, 'Cottage',...
$ price     <i64> 540, 540, 714, 775, 1772, 934, 1947, 1026, 705, 863
$ bedrooms  <i64> 4, 4, 3, None, 4, 3, 4, 3, 4, 3
$ doubles   <i64> 1, 2, 1, 1, 1, 1, 1, 1, 1, 1
$ singles   <i64> 2, 2, 4, 3, 2, 4, 2, 4, 2, 2
$ review    <f64> 8.9, 8.7, 9.2, 9.6, 9.6, 8.6, 9.6, 8.6, 9.7, 9.1
$ beach    <bool> False, True, True, False, True, True, True, True, True, True
Polars 入門

一起來練習吧!

Polars 入門

Preparing Video For Download...