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

除pandas外,是否有支持MultiIndex多级列名的数据表格类库?

支持多级索引(MultiIndex)的工具库

并非只有pandas支持多级索引/多级列名,以下是不同生态中具备该特性的工具:

Python 生态

  • Dask:原生支持MultiIndex(行、列均支持),可直接基于pandas多级索引数据创建,也能手动构建:
    import dask.dataframe as dd
    import pandas as pd
    col_idx = pd.MultiIndex.from_tuples([('GroupA', 'Col1'), ('GroupA', 'Col2'), ('GroupB', 'Col1')], names=['Level1', 'Level2'])
    ddf = dd.from_pandas(pd.DataFrame([[1,2,3]], columns=col_idx), npartitions=1)
    
  • Polars:从0.17版本开始原生支持多级列名,通过元组列表定义列即可:
    import polars as pl
    df = pl.DataFrame(
        [[1, 2, 3, 4]],
        columns=[('Group1', 'Col1'), ('Group1', 'Col2'), ('Group2', 'Col1'), ('Group2', 'Col2')]
    )
    
    访问多级列可使用df[('Group1', 'Col1')]或df['Group1']['Col1']的方式。

R 生态

  • data.table:支持多级列名,可通过setnames直接设置多级命名:
    library(data.table)
    dt <- data.table(a1 = 1:3, a2 = 4:6, b1 = 7:9, b2 = 10:12)
    setnames(dt, c("a1", "a2", "b1", "b2"), list(c("A", "A", "B", "B"), c("X", "Y", "X", "Y")))
    
  • tidyverse:可通过pivot_wider/pivot_longer转换实现多级列的逻辑,或用separate_wider_delim拆分列名模拟多级结构。

Julia 生态

  • DataFrames.jl:原生支持多级列索引(MultiColumnIndex),创建方式直观:
    using DataFrames
    df = DataFrame(
        [1 2 3 4],
        columns = [("Group1", "Col1"), ("Group1", "Col2"), ("Group2", "Col1"), ("Group2", "Col2")]
    )
    
    导入DataFramesMeta后,还可通过df.Group1.Col1简化多级列访问。

其他工具

  • Apache Spark:通过StructType结构化列实现类似多级列的效果,将一组列封装为Struct类型:
    import org.apache.spark.sql.types._
    val schema = StructType(Seq(
        StructField("Group1", StructType(Seq(StructField("Col1", IntegerType), StructField("Col2", IntegerType)))),
        StructField("Group2", StructType(Seq(StructField("Col1", IntegerType), StructField("Col2", IntegerType))))
    ))
    

多数工具并非不支持多级列/索引,只是实现逻辑和pandas略有差异——部分用结构化列模拟,部分则提供原生多级命名支持,只需对应工具的语法规范即可实现类似双层表头的效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 12:25:14