Tabular[data]

从表示行列表的矩形数据创建表格对象.

Tabular[data,{key1,key2,…}]

将 keyi 设为表格对象的列 i 的名称.

更多信息和选项
Details and Options Details and Options
范例  
基本范例  
范围  
创建表格对象  
提取数据  
清理数据  
转换数据  
选项  
Alignment  
Background  
BaselinePosition  
Show More Show More
ImageSize  
ItemDisplayFunction  
ItemSize  
ItemStyle  
Scrollbars  
ScrollPosition  
应用  
属性和关系  
可能存在的问题  
参见
相关指南
历史
按以下格式引用:

Tabular

Tabular[data]

从表示行列表的矩形数据创建表格对象.

Tabular[data,{key1,key2,…}]

将 keyi 设为表格对象的列 i 的名称.

更多信息和选项

  • Tabular 亦称为数据框架、表格和结构化数据.
  • Tabular 通常用于每列可被视为变量、每行可被视为测量值的数据. 通常,仅显示含有部分数据的窗口.
  • 每列都有一个元素类型,例如数字、字符串、日期或表达式. 允许出现数据丢失.
  • data 可能的形式包括:
  • {row1,row2,…}行列表组成的矩阵 »
    {assoc1,assoc2,…}具有共同键的关联列表 »
    SparseArray[…],QuantityArray[…],…特殊矩阵表示 »
    Dataset[…]矩形数据集 »
    TimeSeries[…]一组采样时间-值数据对
    EventSeries[…]一系列时间事件
  • 用 ToTabular 可将更多类型的表达式转换为 Tabular, 并对怎样完成转换有更多的控制.
  • Tabular[data,schm] 可设置或修改表格 data 的架构,其中以 TabularSchema 对象或关联 <|"prop1"val1,…|> 的形式给出 schm.
  • 可用 Tabular[spec] 中以下形式的 spec 创建 SQL 支持的表格对象:
  • RelationalDatabase[…]包含单个表格的关系数据库对象
    RelationalDatabase[…]"table"从关系数据库中选择表格
    <|"RelationalDatabase"RelationalDatabase[…],"Query""table"|>数据库中表格的扩展指定
  • Tabular 可识别缺失值或异常值(如 Missing[]、Null、Infinity 等)并进行操作.
  • 可用 Part、Select 等提取数据元素.
  • 可用 FromTabular 或 Normal 将Tabular 转换成其他形式.
  • Tabular 可与转换函数一起使用,如 TransformColumns 和 AggregateRows.
  • Tabular[tabular,options] 对 Tabular 对象应用给定选项.
  • 以下选项控制 Tabular 的整体外观:
  • AllowedDimensionsAutomatic对行数或列数的限制
    AppearanceElementsAutomatic显示视图中要包含的元素
    BaselinePosition Automatic与周围文本基线对齐
    BaseStyle{}表格的基本样式规范
    ImageMarginsAutomatic显示的表格周围的边距
    ImageSize Automatic表格视图的整体大小
    Scrollbars {Automatic,Automatic}是否包含滚动条
    ScrollPosition Automatic如果启用了滚动,则为滚动位置
  • AppearanceElements 中可能的元素包括 "RowHeaders"、"CollapsedRowHeaders"、"ColumnHeaders"、"CollapsedColumnHeaders"、"Frame" 和 "ResizeArea".
  • Tabular 接受以下选项来确定表格内容的外观:
  • AlignmentAutomatic项目的水平和垂直对齐方式
    BackgroundNone使用什么背景颜色
    DividersAutomatic是否在单元格之间添加分隔线
    HeaderAlignmentAutomatic标题的水平和垂直对齐
    HeaderBackgroundAutomatic用于标题的背景颜色
    HeaderSizeAutomatic表头的宽度和高度
    HeaderStyleAutomatic用于标题的样式
    ItemDisplayFunctionNone用于格式化项目的函数
    ItemSizeAutomatic每个项目的宽度和高度
    ItemStyle{}列和行的样式
  • 内容选项采用 opt<|"elem"spec,…|> 的形式,其中 "elem" 指定 spec 所影响的元素.
  • 可能的元素有:
  • "Columns"{s1,s2,…}按位置设置列的样式
    "Rows"{s1,s2,…}按位置设置行样式
    "ColumnRules"{col1s1,…}按键为列设置样式
    "RowRules"{row1s1,…}按索引为行设置样式
    "ItemRules"{{row1,col1}s1,…}按行和列对样式项进行排列
    "ColumnValueFunction"cf按数值为列设置样式
    "RowValueFunction"rf按数值为行设置样式
    "ItemValueFunction"if按数值对样式项进行设置
  • "Columns" 和 "Rows" 采用以下形式:
  • {s1,s2,…,sn}使用 s1 到 sn,然后使用默认值
    s在所有情况下都使用 s
    Cyclic[{c1,c2,…}]遍历所有 ci
    {s1,s2,…,Cyclic[{c1,c2,…}],sm,…,sn}在开头使用 si 的第一个序列,然后循环使用 ci,最后在结尾使用 si 的最后一个序列
    {s1,s2,…,Cyclic[{}],sm,…,sn}在开头使用 si 的第一个序列,在结尾使用 si 的最后一个序列
  • 列 coli 可以是列键 "key" 或该列的数字索引 i.
  • 行 rowi 可以是该行的数值索引 i,或者,如果 Tabular 包含键列,则可以是 RowKey[{…}].
  • "RowValueFunction"f 将函数 f 应用于数据的每一行,它应该返回一个用于整行的设置,或者返回一个 keyval 结果的列表,指示每一列应使用的设置.
  • "ItemValueFunction" 有以下几种形式:
  • itfn使用 itfn 为每个项目生成设置
    {col1itfn1,…}}使用 itfni 为列 coli 中的项目生成设置
  • 传递给项目值函数的参数包括该项目的值 val,在表格中的位置 {row,col} 以及整个表格对象 tab.
  • "RowValueFunction" 有以下几种形式:
  • rwfn使用 rwfn 为每个项目生成设置
    {col1rwfni,…}}使用 rwfn 为列中的项目生成设置 coli
  • 传递给行值函数的参数包括:行元素的 association assoc 、表格中的行位置 row 以及整个表格对象 tab.

范例

打开所有单元 关闭所有单元

基本范例  (5)

从矩阵创建 Tabular 对象:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}]

从矩阵创建 Tabular 对象,同时指定列键 (column key):

Wolfram Language code: Tabular[{{1, x, Today}, {4, y, Tomorrow}}, {"col1", "col2", "col3"}]

从有共同键的关联列表创建 Tabular 对象:

Wolfram Language code: Tabular[{<|"a" -> 1, "b" -> 2, "c" -> 3|>, <|"a" -> 4, "b" -> 5, "c" -> 6|>}]

根据列数据组成的列表创建 Tabular 对象:

Wolfram Language code: ToTabular[{"a" -> {1, 4}, "b" -> {2, 5}, "c" -> {3, 6}}, "Columns"]

从 Dataset 创建 Tabular 对象:

Wolfram Language code: Tabular[Dataset[{Association["a" -> 1, "b" -> 2, "c" -> 3], Association["a" -> 4, "b" -> 5, "c" -> 6]}]]

范围  (27)

创建表格对象  (11)

从行列表构建 Tabular 对象:

Wolfram Language code: Tabular[{{"M", 8.1, 16.1}, {"M", 13.9, 29.2}, {"M", 16.1, 33.8}, {"F", 7.2, 14.7}, {"F", 14.9, 30.1}}]

从矩阵构建 Tabular 对象,同时指定列的名称:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]

从关联列表构建 Tabular 对象:

Wolfram Language code: Tabular[{<|"name" -> "John", "age" -> 25|>, <|"name" -> "Anne", "age" -> 20|>, <|"name" -> "Rob", "age" -> Missing[]|>}]

从关联列表构建 Tabular 对象,其中含有 ExtendedKey:

Wolfram Language code: Tabular[{<|ExtendedKey["AA", "a"] -> 1, ExtendedKey["AA", "b"] -> 2|>, <|ExtendedKey["AA", "a"] -> 3, ExtendedKey["AA", "b"] -> 4|>}]

从 QuantityArray 创建 Tabular 对象:

Wolfram Language code: qa = QuantityArray[RandomReal[1, {6, 3}], {"Meters", "Seconds", "Grams"}]
Wolfram Language code: Tabular[qa]

从按列排列的列表数据创建的 Tabular 对象需要进行 Transpose:

Wolfram Language code: dates = RandomDate[5, DateGranularity -> "Day"]
Wolfram Language code: values = {1.2, 7.3, 3.8, 4.2, 4.5};
Wolfram Language code: Tabular[Transpose[{dates, values}], {"date", "value"}]

使用 ToTabular:

Wolfram Language code: ToTabular[{"date" -> dates, "value" -> values}, "Columns"]

将 Dataset 转换成 Tabular 对象:

Wolfram Language code: data = Take[ExampleData[{"Dataset", "Planets"}], 4]

转换成 Tabular 将展平数据集的结构:

Wolfram Language code: Tabular[data]

Import "CSV" 数据将自动得到一个 Tabular 对象:

Wolfram Language code: Import["ExampleData/TreesOwnedByTheCityOfChampaign.csv", "Tabular"]

导入一个 "TSV" 文件:

Wolfram Language code: Import["ExampleData/RetailSales.tsv", "Tabular"]

构建数字组成的 Tabular 对象,指定每列元素的类型:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}, {5, 6}}, <|"ElementType" -> {"Integer64", "Integer16"}|>]

检查存储的数据的类型:

Wolfram Language code: ColumnTypes[%]

混合不同类型的列:

Wolfram Language code: Tabular[{{1, "dog", True}, {2, "cow", False}}, <|"ElementType" -> {"Integer8", "String", "Boolean"}|>]

检查存储的数据的类型:

Wolfram Language code: ColumnTypes[%]

显示一个 Tabular 对象,其行对应于实体,其列对应于实体的属性:

Wolfram Language code: EntityValue["Country", {"Area", "HighestElevation", "Population", "PopulationDensity"}, "Tabular"]

提取数据  (9)

提取一个元素:

Wolfram Language code: tab = Tabular[{{1, 2}, {3, 4}}]
Wolfram Language code: tab[[1, 2]]

提取只有一个元素的 Tabular 对象:

Wolfram Language code: tab[[{1}, {2}]]
Wolfram Language code: Normal[%]

提取一行数据:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}}]
Wolfram Language code: %[[1]]
Wolfram Language code: Normal[%]

提取一列数据:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}}]
Wolfram Language code: %[[All, 1]]
Wolfram Language code: Normal[%]

用列键提取一列数据:

Wolfram Language code: tab = Tabular[{<|"a" -> 1, "b" -> 0|>, <|"a" -> 1, "b" -> 1|>, <|"a" -> 2, "b" -> 0|>, <|"a" -> 2, "b" -> 1|>}]
Wolfram Language code: tab[[All, "b"]]
Wolfram Language code: Normal[%]

用 key 列提取 Tabular 对象中的一行数据:

Wolfram Language code: tab = Tabular[{{"one", True, "foo"}, {"two", False, "bar"}}, <|"ColumnKeys" -> {"key", "col1", "col2"}, "KeyColumns" -> {"key"}|>]
Wolfram Language code: tab[Key["one"]]
Wolfram Language code: tab[RowKey["one"]]
Wolfram Language code: tab[<|"key" -> "one"|>]

用几个 key 列提取 Tabular 对象中的一行数据:

Wolfram Language code: tab = Tabular[{{"one", True, "foo"}, {"two", False, "bar"}}, <|"ColumnKeys" -> {"key1", "key2", "col"}, "KeyColumns" -> {"key1", "key2"}|>]
Wolfram Language code: tab[Key[{"one", True}]]
Wolfram Language code: tab[RowKey[{"one", True}]]
Wolfram Language code: tab[<|"key1" -> "one", "key2" -> True|>]

从 Tabular 对象中提取多个列:

Wolfram Language code: tab = ResourceData["Sample Tabular Data: Fisher Iris"]

提取两个列并颠倒它们的顺序:

Wolfram Language code: tab[[All, {3, 2}]]

按列键提取多列数据:

Wolfram Language code: tab[[All, {"Species", "PetalLength", "SepalLength"}]]

用条件选择行:

Wolfram Language code: tab = ResourceData["Sample Tabular Data: Fisher Iris"]

提取 "SepalLength" 大于或等于 6 的所有行:

Wolfram Language code: Select[tab, #SepalLength >= Quantity[7, "Centimeters"]&]

用列的类型选择列:

Wolfram Language code: tab = Tabular[{{"M", 8.1, 16.1}, {"M", 13.9, 29.2}, {"M", 16.1, 33.8}, {"F", 7.2, 14.7}, {"F", 14.9, 30.1}}, {"MF", "val1", "val2"}]

选择数值型的列:

Wolfram Language code: ColumnKeys[tab, #ElementType === "Real64"&]
Wolfram Language code: tab[All, %]

清理数据  (4)

取一个包含国家数据的 Tabular 对象:

Wolfram Language code: tab = EntityValue[EntityClass["Country", "Europe"], {"Population", "Area"}, "Tabular"]

按人口递减排序:

Wolfram Language code: ReverseSortBy[tab, "Population"]

找出面积最小的国家:

Wolfram Language code: MinimalBy[tab, "Area"]

删除一个列:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]
Wolfram Language code: DeleteColumns[%, "col2"]

插入一个列:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]
Wolfram Language code: InsertColumns[%, "col3" -> {"a", "b", "c"}]

重新命名一个列:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]
Wolfram Language code: RenameColumns[%, "col2" -> "c2"]

转换数据  (3)

构建新的列:

Wolfram Language code: times = DateRange[Today, Today + Quantity[4, "Days"]]; values = {1.2, 7.3, 3.8, 4.2, 4.5}; Tabular[Transpose[{times, values}], {"date", "value"}]

将最后一列与新的列保存在一起:

Wolfram Language code: ConstructColumns[%, {"day" -> Function[DayName[#date]], "value"}]

对现有的列进行转换:

Wolfram Language code: dates = {"Sep 1, 2024", "Sep 2, 2024", "Sep 3, 2024"}; values = {1.2, 7.3, 3.8}; Tabular[Transpose[{dates, values}], {"date", "value"}]
Wolfram Language code: TransformColumns[%, "date" -> Function[FromDateString[#date]]]

欧洲国家的人口和面积:

Wolfram Language code: tab = EntityValue[EntityClass["Country", "Europe"], {"Population", "Area"}, "Tabular"]

计算人口密度:

Wolfram Language code: TransformColumns[tab, "Density" -> Function[#Population / #Area]]

选项  (46)

Alignment  (10)

为每一列指定对齐方式:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, Alignment -> <|"Columns" -> {Left, Center, Right}|>]

如果列的数量多于对齐方式的设置数量,后续的列将使用默认对齐方式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"Columns" -> {Center, Left}|>]

在左列和右列对齐方式之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"Columns" -> Cyclic[{Left, Right}]|>]

以居中对齐的列开始和结束,中间的列在左对齐和右对齐之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"Columns" -> {Center, Cyclic[{Left, Right}], Center}|>]

以左对齐列开始和结束,中间使用默认对齐方式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"Columns" -> {Left, Cyclic[{}], Left}|>]

将 "b" 列左对齐:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"ColumnRules" -> {"b" -> Left}|>]

使用其 {row,col} 位置来指定项目的对齐方式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"ItemRules" -> {{3, "b"} -> Left}|>]

如果 "b" 列中的值大于 10,则左对齐各行,否则右对齐:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"RowValueFunction" -> (If[#b > 10, Left, Right]&)|>]

使用 None 可使用默认对齐方式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"RowValueFunction" -> (If[#b > 10, Left, None]&)|>]

如果数值为偶数,则项目左对齐,否则居中对齐:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"ItemValueFunction" -> (If[EvenQ[#], Left, Center]&)|>]

使用 None 可使用默认对齐方式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"ItemValueFunction" -> (If[EvenQ[#], Left, None]&)|>]

根据 "b" 列的数值对其进行对齐:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Alignment -> <|"ItemValueFunction" -> {"b" -> (If[# > 10, Left, Right]&)}|>]

Background  (13)

使用单一样式作为所有项目的背景:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, Background -> RGBColor[0.95, 0.627, 0.1425]]

为每一列指定背景:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, Background -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}|>]

为每一行指定背景:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, Background -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], RGBColor[0.95, 0.627, 0.1425]}|>]

如果列数多于样式,后面的列将使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}|>]

如果行数多于样式,后面的行将使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], RGBColor[0.95, 0.627, 0.1425]}|>]

在蓝色和绿色列背景之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"Columns" -> Cyclic[{RGBColor[0.24, 0.6, 0.8], RGBColor[0.455, 0.7, 0.21]}]|>]

在蓝色和绿色行背景之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}, {"a", "b", "c", "d", "e"}, Background -> <|"Rows" -> Cyclic[{RGBColor[0.24, 0.6, 0.8], RGBColor[0.455, 0.7, 0.21]}]|>]

以蓝色和红色为列背景开始和结束,中间在橙色和绿色之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

以蓝色和红色作为开始和结束的行背景色,在中间行中在橙色和绿色之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}, {"a", "b", "c", "d", "e"}, Background -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

以蓝色和红色列背景作为开始和结束,并在中间使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

以蓝色和红色行背景开始和结束,中间使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}, {"a", "b", "c", "d", "e"}, Background -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

为列 "b" 使用蓝色背景:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"ColumnRules" -> {"b" -> RGBColor[0.24, 0.6, 0.8]}|>]

第二行使用蓝色背景:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"RowRules" -> {2 -> RGBColor[0.24, 0.6, 0.8]}|>]

使用 RowKey[{…}] 来为一行设置样式:

Wolfram Language code: Tabular[{{"apple", 2, 3, 4, 5}, {"banana", 7, 8, 9, 10}, {"grape", 12, 13, 14, 15}}, <|...|>, Background -> <|"RowRules" -> {RowKey[{"banana"}] -> RGBColor[0.24, 0.6, 0.8]}|>]

使用其 {row,col} 位置来指定项目的背景样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"ItemRules" -> {{3, "b"} -> RGBColor[0.24, 0.6, 0.8]}|>]

如果列 "b" 中的值大于 10,则将行背景颜色设为红色,否则设为蓝色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"RowValueFunction" -> (If[#b > 10, RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)|>]

使用 None 可使用默认背景样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"RowValueFunction" -> (If[#b > 10, RGBColor[0.91, 0.318, 0.243], None]&)|>]

根据 "b" 列的值为第二行着色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"RowValueFunction" -> {2 -> (If[#b > 10, RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)}|>]

如果数值为偶数,项目背景颜色为红色,否则为蓝色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"ItemValueFunction" -> (If[EvenQ[#], RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)|>]

使用 None 可使用默认背景样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"ItemValueFunction" -> (If[EvenQ[#], RGBColor[0.91, 0.318, 0.243], None]&)|>]

根据 "b" 列的值为其着色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, Background -> <|"ItemValueFunction" -> {"b" -> (If[# > 10, RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)}|>]

BaselinePosition  (1)

将表格中心与周围文本的基线对齐:

Wolfram Language code: {x, y, Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, BaselinePosition -> Center], z}

将表格底部与基线对齐:

Wolfram Language code: {x, y, Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, BaselinePosition -> Bottom], z}

ImageSize  (5)

默认情况下,宽度受笔记本大小限制,显示的行数也有限:

Wolfram Language code: ResourceData["Sample Tabular Data: Car Models"]

使用命名字体大小以显示更少的 Tabular 对象:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> Small]

限制一个 Tabular 对象的宽度:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> 300]

限制 Tabular 对象的宽度和高度:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> {300, 100}]

限制 Tabular 对象的整体高度:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> {Automatic, 100}]

ItemDisplayFunction  (1)

用 ItemDisplayFunction 改变怎样显示缺失值:

Wolfram Language code: Tabular[{{9, Missing[], 2, 3, 3}, {6, 3, 1, 3, 10}, {1, 7, 4, Missing[], Missing[]}, {5, 7, 6, 4, 3}, {9, 5, 4, 9, 6}}, ItemDisplayFunction -> (If[MissingQ[#], Style["●", StandardRed], #]&)]

ItemSize  (1)

使用 ItemSize 增加项目的高度:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemSize -> 3]

指定项目的宽度和高度:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemSize -> {10, 3}]

使用 Cyclic 为第二列到最后一列分配重复的项大小:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemSize -> {{15, Cyclic[{5}]}, Automatic}]

ItemStyle  (13)

使用单一样式作为所有项目的样式:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> Bold]

使用 Directive 来将多种样式合并为单一的样式:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> Directive[Bold, Italic]]

为每一列指定样式:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> <|"Columns" -> {Bold, Italic, Red}|>]

为每一行指定样式:

Wolfram Language code: Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> <|"Rows" -> {Bold, Red}|>]

如果列数多于样式,后面的列将使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}|>]

如果行数多于样式,后面的行将使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], RGBColor[0.95, 0.627, 0.1425]}|>]

在蓝色和绿色列样式之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Columns" -> Cyclic[{RGBColor[0.24, 0.6, 0.8], RGBColor[0.455, 0.7, 0.21]}]|>]

在蓝色和绿色行样式之间循环:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Rows" -> Cyclic[{RGBColor[0.24, 0.6, 0.8], RGBColor[0.455, 0.7, 0.21]}]|>]

以蓝色和红色的列样式开始和结束,中间在橙色和绿色之间循环切换:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

以蓝色和红色的行样式开始和结束,中间在橙色和绿色之间循环切换:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{RGBColor[0.95, 0.627, 0.1425], RGBColor[0.455, 0.7, 0.21]}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

以蓝色和红色列样式开始和结束,中间使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Columns" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

以蓝色和红色行样式开始和结束,中间使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}, {1, 3, 5, 7, 9}, {2, 4, 6, 8, 10}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"Rows" -> {RGBColor[0.24, 0.6, 0.8], Cyclic[{}], RGBColor[0.922526, 0.385626, 0.209179]}|>]

"b" 栏使用蓝色样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"ColumnRules" -> {"b" -> RGBColor[0.24, 0.6, 0.8]}|>]

第二行使用蓝色样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"RowRules" -> {2 -> RGBColor[0.24, 0.6, 0.8]}|>]

使用 RowKey[{…}] 对行进行样式设置:

Wolfram Language code: Tabular[{{"apple", 2, 3, 4, 5}, {"banana", 7, 8, 9, 10}, {"grape", 12, 13, 14, 15}}, <|...|>, ItemStyle -> <|"RowRules" -> {RowKey[{"banana"}] -> RGBColor[0.24, 0.6, 0.8]}|>]

使用其 {row,col} 位置指定一个条目的样式 style:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"ItemRules" -> {{3, "b"} -> RGBColor[0.24, 0.6, 0.8]}|>]

如果列 "b" 中的值大于 10,则将该行样式设置为红色,否则为蓝色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"RowValueFunction" -> (If[#b > 10, RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)|>]

使用 None 可使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"RowValueFunction" -> (If[#b > 10, RGBColor[0.91, 0.318, 0.243], None]&)|>]

根据 "b" 列的值为第二列着色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"RowValueFunction" -> {2 -> (If[#b > 10, RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)}|>]

如果数值为偶数,则项目颜色为红色,否则为蓝色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"ItemValueFunction" -> (If[EvenQ[#], RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)|>]

使用 None 可使用默认样式:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"ItemValueFunction" -> (If[EvenQ[#], RGBColor[0.91, 0.318, 0.243], None]&)|>]

根据 "b" 列的值为其着色:

Wolfram Language code: Tabular[{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}}, {"a", "b", "c", "d", "e"}, ItemStyle -> <|"ItemValueFunction" -> {"b" -> (If[# > 10, RGBColor[0.91, 0.318, 0.243], RGBColor[0.24, 0.6, 0.8]]&)}|>]

Scrollbars  (1)

当表格大于显示尺寸时,会显示滚动条:

Wolfram Language code: Tabular[RandomReal[1, {20, 15}]]

隐藏列滚动条

Wolfram Language code: Tabular[RandomReal[1, {20, 15}], Scrollbars -> {True, False}]

不使用滚动条:

Wolfram Language code: Tabular[RandomReal[1, {20, 15}], Scrollbars -> False]

ScrollPosition  (1)

默认情况下,滚动条的位置是显示内容的左上角:

Wolfram Language code: Tabular[RandomReal[1, {20, 15}], ScrollPosition -> Automatic]

以点为单位指定滚动条的自定义初始位置:

Wolfram Language code: Tabular[RandomReal[1, {20, 15}], ScrollPosition -> {250, 0}]

ScrollPosition 可以是 Dynamic 对象:

Wolfram Language code: Row[{Column[{Slider[Dynamic[h], {0, 500, 1}], Tabular[RandomReal[1, {20, 15}], ScrollPosition -> Dynamic[{h, v}], Scrollbars -> False, ImageSize -> {400, 300}]}], VerticalSlider[Dynamic[v], {300, 0}]}]

应用  (3)

用 Tabular 对象显示具有单独列标题的列数据:

Wolfram Language code: name = {"Statistics", "FisherIris"};
Wolfram Language code: ExampleData[name, "LongDescription"]

用数组数据和 "ColumnHeadings" 属性创建 Tabular 对象:

Wolfram Language code: tab = Tabular[ExampleData[name], ExampleData[name, "ColumnHeadings"]]

绘制花瓣长度与萼片长度的函数关系图:

Wolfram Language code: ListPlot[tab -> {"SepalLength", "PetalLength"}]

求相关系数:

Wolfram Language code: AggregateRows[tab, "corr" -> Function[Correlation[#SepalLength, #PetalLength]]]

按品种类型计算花朵测量数据的平均值:

Wolfram Language code: AggregateRows[tab, {"SepalLengthMean" -> Function[Mean[#SepalLength]], "SepalWidthMean" -> Function[Mean[#SepalWidth]], "PetalLengthMean" -> Function[Mean[#PetalLength]], "PetalWidthMean" -> Function[Mean[#PetalWidth]]}, "Species"]

以 Tabular 形式给出的 "DuneBooks" 的信息:

Wolfram Language code: EntityValue[EntityClass["Book", "DuneBooks"], {"FirstPublished", "Author", "Image"}, "Tabular"]

用 ItemDisplayFunction 为表格中的列生成分布图,并在图中标记当前项的值.

首先,用 SmoothHistogram 生成分布图:

Wolfram Language code: g = SmoothHistogram[ResourceData["Sample Tabular Data: Car Models"] -> "mpg", PlotStyle -> LightGray, Axes -> None, AspectRatio -> 1 / 3, PlotRangePadding -> Scaled[.05], PlotInteractivity -> False]

创建一个图示函数,在某一项的值处添加标记:

Wolfram Language code: mpgGlyph[v_] := Labeled[Show[g, Epilog -> {RGBColor[0.91, 0.318, 0.243], Thick, InfiniteLine[{{v, 0}, {v, 1}}]}], v, Right]
Wolfram Language code: mpgGlyph[20]

对表格中的列 "mpg" 应用 ItemDisplayFunction:

Wolfram Language code: Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemDisplayFunction -> <|"mpg" -> (mpgGlyph[#]&)|>]

属性和关系  (3)

用 TabularQ 测试 Tabular 对象是否有效:

Wolfram Language code: Tabular[{{1, 2}, {3, 4}}]
Wolfram Language code: TabularQ[%]

以 TabularColumn 对象的形式给出Tabular 对象的列:

Wolfram Language code: tab = Tabular[{{a, b}, {c, d}, {e, f}}]

第一列:

Wolfram Language code: tab[All, 1]
Wolfram Language code: Normal[%]

第二列:

Wolfram Language code: tab[All, 2]
Wolfram Language code: Normal[%]

以 TabularRow 对象的形式给出Tabular 对象的行:

Wolfram Language code: tab = Tabular[{{a, b, c}, {d, e, f}}]

第一行:

Wolfram Language code: tab[1]
Wolfram Language code: Normal[%]

第二行:

Wolfram Language code: tab[2]
Wolfram Language code: Normal[%]

可能存在的问题  (3)

Tabular 输入数据必须至少是二维的:

Wolfram Language code: Tabular[{x, y, z}]

创建单行表格:

Wolfram Language code: Tabular[{{x, y, z}}]

创建单列表格:

Wolfram Language code: Tabular[List /@ {x, y, z}]

不能接受第二层上参差不齐的数据:

Wolfram Language code: Tabular[{{a, b}, {c}}]

数组在前两层上必须是矩形的:

Wolfram Language code: Tabular[{{{a, b}, {c, d}}, {{A, B, X}, {C, D}}}]

可以进行填补:

Wolfram Language code: Tabular[PadRight[{{a, b}, {c}}, Automatic, Missing[]]]

Tabular 对象不能有重复的列键,使用 Part 将自动删除重复的内容:

Wolfram Language code: tab = Tabular[{{1, True, Today}, {2, False, Tomorrow}}, {"col", "bool", "date"}]
Wolfram Language code: tab[[All, {"col", "col"}]]

映射 Part 提取出来的内容,生成相同的列:

Wolfram Language code: tab[[All, #]]& /@ {"col", "col"}

创建没有列键的 Tabular 对象,或使列的键不同:

Wolfram Language code: {Tabular[%], Tabular[%, {"a", "b"}]}
Wolfram Research (2025),Tabular,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Tabular.html (更新于 2025 年).

文本

Wolfram Research (2025),Tabular,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Tabular.html (更新于 2025 年).

CMS

Wolfram 语言. 2025. "Tabular." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2025. https://reference.wolfram.com/language/ref/Tabular.html.

APA

Wolfram 语言. (2025). Tabular. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/Tabular.html 年

BibTeX

@misc{reference.wolfram_2026_tabular, author="Wolfram Research", title="{Tabular}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/Tabular.html}", note=[Accessed: 31-August-2026]}

BibLaTeX

@online{reference.wolfram_2026_tabular, organization={Wolfram Research}, title={Tabular}, year={2025}, url={https://reference.wolfram.com/language/ref/Tabular.html}, note=[Accessed: 31-August-2026]}