跳到主要內容

發表文章

目前顯示的是 2024的文章

Trip for Google Cloud Next '24

Google Cloud Next '24 has a packed schedule. As Google Cloud experts shared incisive speeches and held some brief discussions with them, we deeply felt that artificial intelligence is being further integrated into our daily life and work. (It is really tiring after flying more then 10 hours...)

Rust - 以 JSON 為資料來源建立 Polars DataFrame

緣起 在科技的蓬勃發展中,資料處理和分析變得愈發重要。Rust 語言的 Polars 框架為我們提供了一個強大的工具,讓資料操作變得更加容易且高效。這個框架支援以 JSON 格式匯入資料並建立 Dataframe,為我們提供了一個直觀且方便的方式來操作資料。 本文開始 本文的目的,為使用 JSON 為資料來源,在 Rust 中建立 Polars 的 Dataframe。在以下的文章裡,我們將在 Cargo.toml 中,使用如下的設定: [dependencies] polars = { version = "0.36.2", features = ["json"] } 撰文的當下,Polars 的最新版本為 0.36.2。參考 Polars 的 官方文件 ,它提供了以下的程式碼段落,以匯入 JSON 檔案內容: use polars :: prelude :: * ; let mut file = std :: fs :: File :: open ( "docs/data/path.json" ) . unwrap (); let df = JsonReader :: new ( & mut file ) . finish () . unwrap (); 到這裡,就能輕鬆的利用一個 JSON 檔案的內容,建立出 Dataframe 物件。 好了,故事結束,收工。 . . . . . 如果故事真的就這麼簡單,我想也就沒有撰寫這篇文章的必要了。 想想上面程式段落的例子,過程裡需要透過一個存在於檔案系統中的 JSON 檔案為媒介,儲存我們要處理的資料,再透過「JsonReader」讀入的 Dataframe 中處理。如果這些資料是從網路上取得(例如呼叫RESTful API),以字串變數的形式存在於系統之中,例如下面的內容: let msg = r#"[     {"id": 1, "name": "A", "age": 10},     {"id": 2, "name": "B", "age": 20} ]&