Tabular
更多信息和选项
- 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 的整体外观:
-
AllowedDimensions Automatic 对行数或列数的限制 AppearanceElements Automatic 显示视图中要包含的元素 BaselinePosition Automatic 与周围文本基线对齐 BaseStyle {} 表格的基本样式规范 ImageMargins Automatic 显示的表格周围的边距 ImageSize Automatic 表格视图的整体大小 Scrollbars {Automatic,Automatic} 是否包含滚动条 ScrollPosition Automatic 如果启用了滚动,则为滚动位置 - AppearanceElements 中可能的元素包括 "RowHeaders"、"CollapsedRowHeaders"、"ColumnHeaders"、"CollapsedColumnHeaders"、"Frame" 和 "ResizeArea".
- Tabular 接受以下选项来确定表格内容的外观:
-
Alignment Automatic 项目的水平和垂直对齐方式 Background None 使用什么背景颜色 Dividers Automatic 是否在单元格之间添加分隔线 HeaderAlignment Automatic 标题的水平和垂直对齐 HeaderBackground Automatic 用于标题的背景颜色 HeaderSize Automatic 表头的宽度和高度 HeaderStyle Automatic 用于标题的样式 ItemDisplayFunction None 用于格式化项目的函数 ItemSize Automatic 每个项目的宽度和高度 ItemStyle {} 列和行的样式 - 内容选项采用 opt<|"elem"spec,…|> 的形式,其中 "elem" 指定 spec 所影响的元素.
- 可能的元素有:
-
"Columns"{s1,s2,…} 按位置设置列的样式 "Rows"{s1,s2,…} 按位置设置行样式 "ColumnRules"{col1s1,…} 按键为列设置样式 "RowRules"{row1s1,…} 按索引为行设置样式 "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 应用于数据的每一行,它应该返回一个用于整行的设置,或者返回一个 keyval 结果的列表,指示每一列应使用的设置.
- "ItemValueFunction" 有以下几种形式:
-
itfn 使用 itfn 为每个项目生成设置 {col1itfn1,…}} 使用 itfni 为列 coli 中的项目生成设置 - 传递给项目值函数的参数包括该项目的值 val,在表格中的位置 {row,col} 以及整个表格对象 tab.
- "RowValueFunction" 有以下几种形式:
-
rwfn 使用 rwfn 为每个项目生成设置 {col1rwfni,…}} 使用 rwfn 为列中的项目生成设置 coli - 传递给行值函数的参数包括:行元素的 association assoc 、表格中的行位置 row 以及整个表格对象 tab.
范例
打开所有单元 关闭所有单元基本范例 (5)
从矩阵创建 Tabular 对象:
Tabular[{{1, 2, 3}, {4, 5, 6}}]从矩阵创建 Tabular 对象,同时指定列键 (column key):
Tabular[{{1, x, Today}, {4, y, Tomorrow}}, {"col1", "col2", "col3"}]从有共同键的关联列表创建 Tabular 对象:
Tabular[{<|"a" -> 1, "b" -> 2, "c" -> 3|>, <|"a" -> 4, "b" -> 5, "c" -> 6|>}]根据列数据组成的列表创建 Tabular 对象:
ToTabular[{"a" -> {1, 4}, "b" -> {2, 5}, "c" -> {3, 6}}, "Columns"]Tabular[Dataset[{Association["a" -> 1, "b" -> 2, "c" -> 3], Association["a" -> 4, "b" -> 5, "c" -> 6]}]]范围 (27)
创建表格对象 (11)
从行列表构建 Tabular 对象:
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 对象,同时指定列的名称:
Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]从关联列表构建 Tabular 对象:
Tabular[{<|"name" -> "John", "age" -> 25|>, <|"name" -> "Anne", "age" -> 20|>, <|"name" -> "Rob", "age" -> Missing[]|>}]从关联列表构建 Tabular 对象,其中含有 ExtendedKey:
Tabular[{<|ExtendedKey["AA", "a"] -> 1, ExtendedKey["AA", "b"] -> 2|>, <|ExtendedKey["AA", "a"] -> 3, ExtendedKey["AA", "b"] -> 4|>}]从 QuantityArray 创建 Tabular 对象:
qa = QuantityArray[RandomReal[1, {6, 3}], {"Meters", "Seconds", "Grams"}]Tabular[qa]从按列排列的列表数据创建的 Tabular 对象需要进行 Transpose:
dates = RandomDate[5, DateGranularity -> "Day"]values = {1.2, 7.3, 3.8, 4.2, 4.5};Tabular[Transpose[{dates, values}], {"date", "value"}]使用 ToTabular:
ToTabular[{"date" -> dates, "value" -> values}, "Columns"]data = Take[ExampleData[{"Dataset", "Planets"}], 4]转换成 Tabular 将展平数据集的结构:
Tabular[data]Import "CSV" 数据将自动得到一个 Tabular 对象:
Import["ExampleData/TreesOwnedByTheCityOfChampaign.csv", "Tabular"]导入一个 "TSV" 文件:
Import["ExampleData/RetailSales.tsv", "Tabular"]构建数字组成的 Tabular 对象,指定每列元素的类型:
Tabular[{{1, 2}, {3, 4}, {5, 6}}, <|"ElementType" -> {"Integer64", "Integer16"}|>]ColumnTypes[%]Tabular[{{1, "dog", True}, {2, "cow", False}}, <|"ElementType" -> {"Integer8", "String", "Boolean"}|>]ColumnTypes[%]显示一个 Tabular 对象,其行对应于实体,其列对应于实体的属性:
EntityValue["Country", {"Area", "HighestElevation", "Population", "PopulationDensity"}, "Tabular"]提取数据 (9)
tab = Tabular[{{1, 2}, {3, 4}}]tab[[1, 2]]提取只有一个元素的 Tabular 对象:
tab[[{1}, {2}]]Normal[%]Tabular[{{1, 2}, {3, 4}}]%[[1]]Normal[%]Tabular[{{1, 2}, {3, 4}}]%[[All, 1]]Normal[%]tab = Tabular[{<|"a" -> 1, "b" -> 0|>, <|"a" -> 1, "b" -> 1|>, <|"a" -> 2, "b" -> 0|>, <|"a" -> 2, "b" -> 1|>}]tab[[All, "b"]]Normal[%]用 key 列提取 Tabular 对象中的一行数据:
tab = Tabular[{{"one", True, "foo"}, {"two", False, "bar"}}, <|"ColumnKeys" -> {"key", "col1", "col2"}, "KeyColumns" -> {"key"}|>]tab[Key["one"]]tab[RowKey["one"]]tab[<|"key" -> "one"|>]用几个 key 列提取 Tabular 对象中的一行数据:
tab = Tabular[{{"one", True, "foo"}, {"two", False, "bar"}}, <|"ColumnKeys" -> {"key1", "key2", "col"}, "KeyColumns" -> {"key1", "key2"}|>]tab[Key[{"one", True}]]tab[RowKey[{"one", True}]]tab[<|"key1" -> "one", "key2" -> True|>]从 Tabular 对象中提取多个列:
tab = ResourceData["Sample Tabular Data: Fisher Iris"]tab[[All, {3, 2}]]tab[[All, {"Species", "PetalLength", "SepalLength"}]]tab = ResourceData["Sample Tabular Data: Fisher Iris"]提取 "SepalLength" 大于或等于 6 的所有行:
Select[tab, #SepalLength >= Quantity[7, "Centimeters"]&]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"}]ColumnKeys[tab, #ElementType === "Real64"&]tab[All, %]清理数据 (4)
取一个包含国家数据的 Tabular 对象:
tab = EntityValue[EntityClass["Country", "Europe"], {"Population", "Area"}, "Tabular"]ReverseSortBy[tab, "Population"]MinimalBy[tab, "Area"]Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]DeleteColumns[%, "col2"]Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]InsertColumns[%, "col3" -> {"a", "b", "c"}]Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"col1", "col2"}]RenameColumns[%, "col2" -> "c2"]转换数据 (3)
times = DateRange[Today, Today + Quantity[4, "Days"]];
values = {1.2, 7.3, 3.8, 4.2, 4.5};
Tabular[Transpose[{times, values}], {"date", "value"}]ConstructColumns[%, {"day" -> Function[DayName[#date]], "value"}]dates = {"Sep 1, 2024", "Sep 2, 2024", "Sep 3, 2024"};
values = {1.2, 7.3, 3.8};
Tabular[Transpose[{dates, values}], {"date", "value"}]TransformColumns[%, "date" -> Function[FromDateString[#date]]]tab = EntityValue[EntityClass["Country", "Europe"], {"Population", "Area"}, "Tabular"]TransformColumns[tab, "Density" -> Function[#Population / #Area]]选项 (46)
Alignment (10)
Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, Alignment -> <|"Columns" -> {Left, Center, Right}|>]如果列的数量多于对齐方式的设置数量,后续的列将使用默认对齐方式:
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}|>]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}]|>]以居中对齐的列开始和结束,中间的列在左对齐和右对齐之间循环:
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}|>]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}|>]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}|>]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,则左对齐各行,否则右对齐:
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 可使用默认对齐方式:
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]&)|>]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 可使用默认对齐方式:
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]&)|>]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)
Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, Background -> RGBColor[0.95, 0.627, 0.1425]]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]}|>]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]}|>]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]}|>]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]}|>]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]}]|>]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]}]|>]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]}|>]以蓝色和红色作为开始和结束的行背景色,在中间行中在橙色和绿色之间循环:
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]}|>]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]}|>]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]}|>]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]}|>]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[{…}] 来为一行设置样式:
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]}|>]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,则将行背景颜色设为红色,否则设为蓝色:
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 可使用默认背景样式:
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]&)|>]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]]&)}|>]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 可使用默认背景样式:
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]&)|>]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)
ImageSize (5)
ResourceData["Sample Tabular Data: Car Models"]使用命名字体大小以显示更少的 Tabular 对象:
Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> Small]限制一个 Tabular 对象的宽度:
Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> 300]限制 Tabular 对象的宽度和高度:
Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> {300, 100}]限制 Tabular 对象的整体高度:
Tabular[ResourceData["Sample Tabular Data: Car Models"], ImageSize -> {Automatic, 100}]ItemDisplayFunction (1)
用 ItemDisplayFunction 改变怎样显示缺失值:
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 增加项目的高度:
Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemSize -> 3]Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemSize -> {10, 3}]使用 Cyclic 为第二列到最后一列分配重复的项大小:
Tabular[ResourceData["Sample Tabular Data: Car Models"], ItemSize -> {{15, Cyclic[{5}]}, Automatic}]ItemStyle (13)
Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> Bold]使用 Directive 来将多种样式合并为单一的样式:
Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> Directive[Bold, Italic]]Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> <|"Columns" -> {Bold, Italic, Red}|>]Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}, ItemStyle -> <|"Rows" -> {Bold, Red}|>]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]}|>]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]}|>]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]}]|>]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]}]|>]以蓝色和红色的列样式开始和结束,中间在橙色和绿色之间循环切换:
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]}|>]以蓝色和红色的行样式开始和结束,中间在橙色和绿色之间循环切换:
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]}|>]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]}|>]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]}|>]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]}|>]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[{…}] 对行进行样式设置:
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:
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,则将该行样式设置为红色,否则为蓝色:
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 可使用默认样式:
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]&)|>]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]]&)}|>]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 可使用默认样式:
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]&)|>]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)
ScrollPosition (1)
Tabular[RandomReal[1, {20, 15}], ScrollPosition -> Automatic]Tabular[RandomReal[1, {20, 15}], ScrollPosition -> {250, 0}]ScrollPosition 可以是 Dynamic 对象:
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 对象显示具有单独列标题的列数据:
name = {"Statistics", "FisherIris"};ExampleData[name, "LongDescription"]用数组数据和 "ColumnHeadings" 属性创建 Tabular 对象:
tab = Tabular[ExampleData[name], ExampleData[name, "ColumnHeadings"]]ListPlot[tab -> {"SepalLength", "PetalLength"}]AggregateRows[tab, "corr" -> Function[Correlation[#SepalLength, #PetalLength]]]AggregateRows[tab, {"SepalLengthMean" -> Function[Mean[#SepalLength]], "SepalWidthMean" -> Function[Mean[#SepalWidth]], "PetalLengthMean" -> Function[Mean[#PetalLength]], "PetalWidthMean" -> Function[Mean[#PetalWidth]]}, "Species"]以 Tabular 形式给出的 "DuneBooks" 的信息:
EntityValue[EntityClass["Book", "DuneBooks"], {"FirstPublished", "Author", "Image"}, "Tabular"]用 ItemDisplayFunction 为表格中的列生成分布图,并在图中标记当前项的值.
首先,用 SmoothHistogram 生成分布图:
g = SmoothHistogram[ResourceData["Sample Tabular Data: Car Models"] -> "mpg", PlotStyle -> LightGray, Axes -> None, AspectRatio -> 1 / 3, PlotRangePadding -> Scaled[.05], PlotInteractivity -> False]mpgGlyph[v_] := Labeled[Show[g, Epilog -> {RGBColor[0.91, 0.318, 0.243], Thick, InfiniteLine[{{v, 0}, {v, 1}}]}], v, Right]mpgGlyph[20]对表格中的列 "mpg" 应用 ItemDisplayFunction:
Tabular[ResourceData["Sample Tabular Data: Car Models"],
ItemDisplayFunction -> <|"mpg" -> (mpgGlyph[#]&)|>]属性和关系 (3)
Tabular[{{1, 2}, {3, 4}}]TabularQ[%]以 TabularColumn 对象的形式给出Tabular 对象的列:
tab = Tabular[{{a, b}, {c, d}, {e, f}}]tab[All, 1]Normal[%]tab[All, 2]Normal[%]以 TabularRow 对象的形式给出Tabular 对象的行:
tab = Tabular[{{a, b, c}, {d, e, f}}]tab[1]Normal[%]tab[2]Normal[%]可能存在的问题 (3)
Tabular 输入数据必须至少是二维的:
Tabular[{x, y, z}]Tabular[{{x, y, z}}]Tabular[List /@ {x, y, z}]Tabular[{{a, b}, {c}}]Tabular[{{{a, b}, {c, d}}, {{A, B, X}, {C, D}}}]Tabular[PadRight[{{a, b}, {c}}, Automatic, Missing[]]]Tabular 对象不能有重复的列键,使用 Part 将自动删除重复的内容:
tab = Tabular[{{1, True, Today}, {2, False, Tomorrow}}, {"col", "bool", "date"}]tab[[All, {"col", "col"}]]映射 Part 提取出来的内容,生成相同的列:
tab[[All, #]]& /@ {"col", "col"}创建没有列键的 Tabular 对象,或使列的键不同:
{Tabular[%], Tabular[%, {"a", "b"}]}文本
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]}