ConstructColumns[tab,{col1,col2,…}]
通过从表格数据 tab 中提取列 coli 构造新的表格数据.
ConstructColumns[tab,{ncol1f1,ncol2f2,…}]
返回新的表格数据,其中列 ncoli 由对 tab 的每一行应用函数 fi 生成.
ConstructColumns[cspec]
表示 ConstructColumns 的运算符形式,可以应用于表格数据.
ConstructColumns
ConstructColumns[tab,{col1,col2,…}]
通过从表格数据 tab 中提取列 coli 构造新的表格数据.
ConstructColumns[tab,{ncol1f1,ncol2f2,…}]
返回新的表格数据,其中列 ncoli 由对 tab 的每一行应用函数 fi 生成.
ConstructColumns[cspec]
表示 ConstructColumns 的运算符形式,可以应用于表格数据.
更多信息和选项
- ConstructColumns 也称为创建列(create column).
- ConstructColumns 通常用于提取或构造新的数据列,丢弃所有未指定的内容.
- 表格数据 tab 的可能形式包括:
-
Tabular[…] 类型一致的表格数据 Dataset[…] 通用的分层数据 TimeSeries[…] 采样时间-值数据对的集合 EventSeries[…] 一系列时间事件 {assoc1,assoc2,…} 具有相同键的关联列表 matrix 数值矩阵 - 混合列规范(例如 {…,col,ncolf,…})表示新列 ncol 必须放置在结果中列 col 之后. 列 col 被称为插入新列的锚定列.
- 对于匿名表格数据 tab,ConstructColumns[tab,{f1,f2,…}] 返回新的表格数据,其中第 i 列通过对 tab 的每一行应用函数 fi 构造.
- 函数 fi 被应用于输入表格数据 tab 的单个行,如果 tab 具有列键,则行表示为关联 <|col1val1,…|>,如果 tab 没有列键,则行表示为列表 {val1,…}.
- ConstructColumns[tab,col] 等价于 ConstructColumns[tab,{col}].
- ConstructColumns[cspec][tab] 等价于 ConstructColumns[tab,cspec].
范例
打开所有单元 关闭所有单元基本范例 (2)
在新列 "c" 中构造两列 "a" 和 "b" 之间的差值:
tab = Tabular[{{2, 2.78}, {1, 3.14}, {3, 1.68}}, {"a", "b"}]ConstructColumns[tab, "c" -> Function[#a - #b]]ConstructColumns[tab, {"a", "c" -> (#a - #b&)}]ConstructColumns[tab, {"c" -> (#a - #b&), "a"}]从 Tabular 对象中提取单个列:
Tabular[{{1, 2}, {3, 4}, {5, 6}}, {"a", "b"}]ConstructColumns[%, "b"]范围 (13)
提取 (7)
从 Tabular 对象中提取一列:
tab = Tabular[{{-1, Yesterday}, {0, Today}, {1, Tomorrow}}, {"n", "date"}]ConstructColumns[tab, "date"]ConstructColumns[tab, {"date"}]Part 使用 "date" 或{"date"} 提取会返回不同的结果:
tab[[All, "date"]]tab[[All, {"date"}]]从 Tabular 对象中提取几列:
tab = Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}]ConstructColumns[tab, {"a", "c"}]这等价于使用 Part 提取:
tab[[All, {"a", "c"}]]ConstructColumns 保留原来的列顺序,但 Part 使用新的给定顺序:
ConstructColumns[tab, {"c", "a"}]tab[[All, {"c", "a"}]]tab = Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}]ConstructColumns[tab, {"a" -> Function[#a], "C" -> Function[#c]}]ConstructColumns[tab, {"c" -> Function[#c], "a" -> Function[#a]}]ConstructColumns[tab, {"C" -> Function[#c], "A" -> Function[#a]}]使用 ConstructColumns 的运算符形式来提取列:
Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}]%//ConstructColumns[{"b", "c"}]从 Dataset 对象中提取列:
ds = Dataset[{<|"a" -> 1, "b" -> 2, "c" -> 3|>, <|"a" -> 4, "b" -> 5, "c" -> 6|>}]ConstructColumns[ds, "b"]ConstructColumns[ds, {"a", "c"}]assocs = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "c" -> 4|>};ConstructColumns[assocs, "a"]任何行中缺少的项都将被报告为 Missing 表达式:
ConstructColumns[assocs, "b"]ConstructColumns[assocs, {"a", "c"}]使用 ConstructColumns 的第二个参数中的函数列表从矩阵中提取列:
MatrixForm[mat = {{1, 2, 3, 4}, {5, 6, 7, 8}}]ConstructColumns[mat, {First, Extract[3]}]//MatrixFormConstructColumns[mat, {#[[1]]&, #[[3]]&}]//MatrixFormConstructColumns[mat, {Apply[#1&], Apply[#3&]}]//MatrixFormPart 提取提供了更直接的语法:
mat[[All, {1, 3}]]//MatrixForm创建 (3)
取一个包含三列的 Tabular 对象:
tab = Tabular[{{1, 2, 3}, {4, 5, 6}}, {"a", "b", "c"}]构造另一个 Tabular 对象,其中包含使用给定函数计算的两列:
ConstructColumns[tab, {"b+a" -> Function[#b + #a], "b-a" -> Function[#b - #a]}]ConstructColumns[tab, {"total" -> Total, "echo" -> Echo}]取一个具有三列且没有列键的 Tabular 对象:
tab = Tabular[{{1, 2, 3}, {4, 5, 6}}]构造另一个 Tabular 对象,其列由行函数列表计算得出:
ConstructColumns[tab, {#[[2]] + #[[1]]&, #[[2]] - #[[1]]&}]使用 Apply[f] 而不是 f 来处理具有单独编号槽位的各列:
ConstructColumns[tab, {Apply[#2 + #1&], Apply[#2 - #1&]}]ConstructColumns[tab, {Total, Echo}]assocs = {<|"a" -> 1, "b" -> 2|>, <|"a" -> 3, "b" -> 4|>};ConstructColumns[assocs, "c" -> Function[#a ^ 2]]列操作 (3)
使用 ColumnwiseValue 从列值中减去该列的平均值:
tab = ToTabular[<|"c" -> {3.71, -2.7, -5.29, 9.33, 6.26}|>, "Columns"]ConstructColumns[tab, "c-μ" -> (#c - ColumnwiseValue[Mean[#c]]&)]mean = Mean[tab[[All, 1]]]ConstructColumns[tab, "c-μ" -> (#c - mean&)]查找此 Tabular 对象的某一列中哪些元素大于中位数:
ToTabular[<|"c" -> {4.24, 1.14, 7.38, -6.74, 7.5}|>, "Columns"]ConstructColumns[%, "above median" -> (Echo[#c > ColumnwiseValue[Echo[Median[#c], "median:"]], #RowNumber]&)]使用 ColumnwiseThread 计算整个列的向量值变换:
tab = ToTabular[<|"v" -> {2.07, -0.75, 4.16, 7.28, 8.63, 7.93}|>, "Columns"];
ConstructColumns[tab, "z" -> (ColumnwiseThread[Accumulate[#v]]&)]使用 ColumnwiseValue,为每一行生成相同的列表:
ConstructColumns[tab, "zlist" -> (ColumnwiseValue[Accumulate[#v]]&)]应用 (3)
气象数据 (1)
来自肯尼迪(JFK)机场的气象数据(单位:摄氏度、毫巴和公里/小时):
data = Tabular[IconizedObject[«JFK weather»]]data1 = Discard[data, Count[#, _Missing] > 0&]chill[t_, v_] := If[t > 10 || v < 5, t, 13.12 + 0.6215 * t + (0.3965 * t - 11.37) * (v ^ 0.16), Missing[]];创建一个 Tabular 对象,其中包含一个新列,以及从原始数据中选择的几个特定的列:
ConstructColumns[data1, {"date", "temperature", "wind_chill" -> Function[chill[#temperature, #"wind_speed"]]}]DateListPlot[% -> {{"date", "temperature"}, {"date", "wind_chill"}}, PlotLegends -> {"temperature", "wind_chill"}]臭氧读数 (1)
取一个包含洛杉矶每月臭氧读数数据的 Tabular 对象:
tab = ResourceData["Sample Tabular Data: Los Angeles Ozone"]With[{noYear = KeyDrop["Year"]}, ConstructColumns[tab, {"Year", "Mean" -> Mean @* noYear, "SD" -> StandardDeviation @* noYear, "Q1" -> (Quantile[#, .25]& @* noYear), "Median" -> Median @* noYear, "Q3" -> (Quantile[#, .75]& @* noYear)}]]ListLinePlot[% -> {{"Year", "Median"}, {"Year", "Q1"}, {"Year", "Q3"}}, PlotStyle -> {Automatic, LightGray, LightGray}, Filling -> 2 -> {3}, AxesLabel -> {"year", "pphm"}, PlotLegends -> {"Median", "Q1-Q3"}]树木数据 (1)
tab = ResourceData["Sample Tabular Data: NYC Trees"]ColumnKeys[tab]使用 ConstructColumns 将 "latitude" 和 "longitude" 列合并为一列 GeoPosition 对象:
ConstructColumns[tab, "geoposition" -> Function[GeoPosition[{#latitude, #longitude}]]]GeoGraphics[Point[Normal[%[ ;; 1000, "geoposition"]]]]属性和关系 (2)
ConstructColumns 是 DeleteColumns 的互补操作:
tab = Tabular[Association["RawSchema" -> Association["ColumnProperties" ->
Association["a" -> Association["ElementType" -> "Integer64"],
"b" -> Association["ElementType" -> "Integer64"],
"c" -> Association["ElementType" -> "Integer64"]], "KeyColumns" -> None,
"Backend" -> "WolframKernel"], "Options" -> {},
"BackendData" -> Association["ColumnData" -> DataStructure["ColumnTable",
{{TabularColumn[Association["Data" -> {{5, 6, 9}, {}, None}, "ElementType" -> "Integer64"]],
TabularColumn[Association["Data" -> {{2, 1, 2}, {}, None}, "ElementType" -> "Integer64"]],
TabularColumn[Association["Data" -> {{3, 7, 5}, {}, None}, "ElementType" ->
"Integer64"]]}}]]]];ConstructColumns[tab, "b"]DeleteColumns[tab, {"a", "c"}]ConstructColumns 仅保留列出的列:
tab = Tabular[Association["RawSchema" -> Association["ColumnProperties" ->
Association["a" -> Association["ElementType" -> "Integer64"],
"b" -> Association["ElementType" -> "Integer64"],
"c" -> Association["ElementType" -> "Integer64"]], "KeyColumns" -> None,
"Backend" -> "WolframKernel"], "Options" -> {},
"BackendData" -> Association["ColumnData" -> DataStructure["ColumnTable",
{{TabularColumn[Association["Data" -> {{1, 3, 5}, {}, None}, "ElementType" -> "Integer64"]],
TabularColumn[Association["Data" -> {{2, 4, 6}, {}, None}, "ElementType" -> "Integer64"]],
TabularColumn[Association["Data" -> {{3, 7, 11}, {}, None}, "ElementType" ->
"Integer64"]]}}]]]];ConstructColumns[tab, "b" -> Function[#b * 2]]TransformColumns 保持所有列不被转换:
TransformColumns[tab, "b" -> Function[#b * 2]]文本
Wolfram Research (2025),ConstructColumns,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ConstructColumns.html (更新于 2026 年).
CMS
Wolfram 语言. 2025. "ConstructColumns." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2026. https://reference.wolfram.com/language/ref/ConstructColumns.html.
APA
Wolfram 语言. (2025). ConstructColumns. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/ConstructColumns.html 年
BibTeX
@misc{reference.wolfram_2026_constructcolumns, author="Wolfram Research", title="{ConstructColumns}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/ConstructColumns.html}", note=[Accessed: 08-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_constructcolumns, organization={Wolfram Research}, title={ConstructColumns}, year={2026}, url={https://reference.wolfram.com/language/ref/ConstructColumns.html}, note=[Accessed: 08-September-2026]}