Rust polars库 DataFrame

文章目录


polars 库文档查看 【点击】 数据结构与特征:

数据读写 CSV/ JSON

点击查看更多示例

在使用相应的模块时,需要开启此特性,否则该方法无法使用。
则会出现报错:use of undeclared type 'JsonReader'

[dependencies]
polars = { version = "0.10.1", features = ["json"] }

一些方法的使用:

use polars::prelude::*;
use std::io::Cursor;
use polars::df;


// 先创建Series 再创建DataFrame
fn new() -> Result<DataFrame>{
    let s0 = Series::new("days", [0, 1, 2].as_ref());
    let s1 = Series::new("temp", [22.1, 19.9, 7.].as_ref());
    let s2 = Series::new("unit", &["liang", "jin", "dun"]);
    let s3 = Series::new("sale", &[8u8, 122, 255]);

    DataFrame::new(vec![s0, s1,s2,s3])
}

// 直接使用df! 创建DataFrame
fn new_join() -> Result<DataFrame> {
    let temp = df!("days" => &[0, 1, 2, 3, 4],
                   "temp" => &[22.1, 19.9, 7., 2., 3.])?;

    let rain = df!("days" => &[1, 2],
                   "rain" => &[0.1, 0.2])?;

    temp.left_join(&rain, "days", "days")
}


// 读取json并解析成DataFrame
fn parser_test() -> Result<DataFrame>{
    let base_json = r#"{"a":1, "b":2.0, "c":false, "d":"4"}
                            {"a":-10, "b":-3.5, "c":true, "d":"4"}
                            {"a":2, "b":0.6, "c":false, "d":"text"}
                            {"a":7, "b":-3.5, "c":true, "d":"4"}
                            {"a":1, "b":0.6, "c":false, "d":"text"}
                            {"a":1, "b":0.6, "c":false, "d":"text"}
                            {"a":1, "b":-3.5, "c":true, "d":"4"}
                            {"a":100000000000000, "b":0.6, "c":false, "d":"text"}"#;

    let file = Cursor::new(base_json);
    let df_01 = JsonReader::new(file)
        .infer_schema(Some(3))   // 使用前几行用于数据类型的推断
        .with_batch_size(3)      //  每次读取数据批次大小
        .finish();
    df_01
}


fn main() {
    let df_01 = parser_test();
    let df_02 = new();
    let df_03 = new_join();

    println!("df_01 :\n{:?}",df_01.unwrap());
    println!("df_02 :\n{:?}",df_02.unwrap());
    println!("df_03 :\n{:?}",df_03.unwrap());


    let s0 = Series::new("a", &[1i8, 2, 3]);
    let s1 = Series::new("b", &[1i8, 1, 1]);
    let s2 = Series::new("c", &[2i8, 2, 2]);
    let list = Series::new("foo", &[s0, s1, s2]);

    let s0 = Series::new("B", [1, 2, 3]);
    let s1 = Series::new("C", [1, 1, 1]);
    let df_04 = DataFrame::new(vec![list, s0, s1]).unwrap();
    let df_exploded = df_04.explode("foo").unwrap();

    println!("{:?}", df_04);
    println!("{:?}", df_exploded);

}

拓展阅读:
polars-book
lazy_py-polars文档查看
polars-rust

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页