You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Vec<Vec<f64>>作为Series创建Polars DataFrame

Rust 实现(使用 Polars 库)

Polars 是 Rust 生态中适配嵌套数组类型的常用 DataFrame 库,能满足存储Vec<Vec<f64>>结构的需求。

首先在Cargo.toml中添加依赖:

[dependencies]
polars = { version = "0.35", features = ["lazy", "ndarray"] }

编写实现代码:

use polars::prelude::*;

fn main() {
    // 构造各列数据
    let timestamps = Series::new("timestamp", &[1598215600, 1598222400, 1598229200]);
    
    let bids = Series::new(
        "bids",
        &[
            vec![vec![10.0, 20.0], vec![15.0, 30.0]],
            vec![vec![11.0, 25.0], vec![16.0, 35.0]],
            vec![vec![12.0, 30.0], vec![18.0, 40.0]],
        ],
    );
    
    let asks = Series::new(
        "asks",
        &[
            vec![vec![20.0, 10.0], vec![25.0, 20.0]],
            vec![vec![22.0, 15.0], vec![28.0, 25.0]],
            vec![vec![24.0, 20.0], vec![30.0, 30.0]],
        ],
    );
    
    let tickers = Series::new("ticker", &["AAPL", "MSFT", "GOOG"]);

    // 构建 DataFrame
    let df = DataFrame::new(vec![timestamps, bids, asks, tickers]).unwrap();
    
    // 打印验证结果
    println!("{}", df);
}
Python 实现(使用 Pandas 库)

Pandas 支持直接存储嵌套列表结构,无需额外配置即可实现需求:

import pandas as pd

# 构造数据源
data = {
    "timestamp": [1598215600, 1598222400, 1598229200],
    "bids": [[[10, 20], [15, 30]], [[11, 25], [16, 35]], [[12, 30], [18, 40]]],
    "asks": [[[20, 10], [25, 20]], [[22, 15], [28, 25]], [[24, 20], [30, 30]]],
    "ticker": ["AAPL", "MSFT", "GOOG"]
}

# 生成目标 DataFrame
df = pd.DataFrame(data)

# 打印验证结果
print(df)

内容的提问来源于stack exchange,提问作者ripbozo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:21:23