Function Repository Resource:

MapAtColumns

Source Notebook

Apply functions to elements of specific columns in rectangular data

Contributed by: Sjoerd Smit

ResourceFunction["MapAtColumns"][f,tab]

applies function f to the elements all column in Tabular data tab.

ResourceFunction["MapAtColumns"][{col1f1,}, tab]

applies different functions to different columns.

ResourceFunction["MapAtColumns"][rules]

represents an operator form that can be applied to Tabular data.

Details

The specified columns coli must be present in the data.
ResourceFunction["MapAtColumns"] uses TransformColumns and works on the same types of data, including Association and Dataset expressions.
Unlike TransformColumns, the functions specified in ResourceFunction["MapAtColumns"] do not refer to column names.

Examples

Basic Examples (3) 

Round up the values in all columns:

In[1]:=
tab = Tabular[{{2., 2.78}, {1., 3.14}, {3.5, 1.68}}, {"a", "b"}]
Out[1]=
In[2]:=
ResourceFunction["MapAtColumns"][Ceiling, tab]
Out[2]=

Round up only a specific column:

In[3]:=
ResourceFunction["MapAtColumns"]["b" -> Ceiling, tab]
Out[3]=

Apply different functions to different columns:

In[4]:=
ResourceFunction["MapAtColumns"][{"a" -> Sqrt, "b" -> Ceiling}, tab]
Out[4]=

Scope (4) 

Use MapAtColumns as an operator:

In[5]:=
tab = 
Tabular[<|"RawSchema" -> <|"ColumnProperties" -> <|"a" -> <|"ElementType" -> "Real64"|>, "b" -> <|"ElementType" -> "Real64"|>|>, "KeyColumns" -> None, "Backend" -> "WolframKernel"|>, "BackendData" -> <|"ColumnData" -> DataStructure["ColumnTable", {{
TabularColumn[<|"Data" -> {{2., 1., 3.5}, {}, None}, "ElementType" -> "Real64"|>], 
TabularColumn[<|"Data" -> {{2.78, 3.14, 1.68}, {}, None}, "ElementType" -> "Real64"|>]}}]|>|>];
tab // ResourceFunction["MapAtColumns"][ "b" -> Floor]
Out[6]=

Use ColumnwiseValue to subtract the Mean from a columns:

In[7]:=
tab = 
Tabular[<|"RawSchema" -> <|"ColumnProperties" -> <|"a" -> <|"ElementType" -> "Real64"|>, "b" -> <|"ElementType" -> "Real64"|>|>, "KeyColumns" -> None, "Backend" -> "WolframKernel"|>, "BackendData" -> <|"ColumnData" -> DataStructure["ColumnTable", {{
TabularColumn[<|"Data" -> {{2., 1., 3.5}, {}, None}, "ElementType" -> "Real64"|>], 
TabularColumn[<|"Data" -> {{2.78, 3.14, 1.68}, {}, None}, "ElementType" -> "Real64"|>]}}]|>|>];
ResourceFunction["MapAtColumns"][
 Function[# - ColumnwiseValue[Mean[#]]], tab]
Out[8]=
In[9]:=
ResourceFunction["MapAtColumns"][
 "b" -> Function[# - ColumnwiseValue[Mean[#]]], tab]
Out[9]=

Accumulate a column:

In[10]:=
tab = 
Tabular[<|"RawSchema" -> <|"ColumnProperties" -> <|"a" -> <|"ElementType" -> "Real64"|>, "b" -> <|"ElementType" -> "Real64"|>|>, "KeyColumns" -> None, "Backend" -> "WolframKernel"|>, "BackendData" -> <|"ColumnData" -> DataStructure["ColumnTable", {{
TabularColumn[<|"Data" -> {{2., 1., 3.5}, {}, None}, "ElementType" -> "Real64"|>], 
TabularColumn[<|"Data" -> {{2.78, 3.14, 1.68}, {}, None}, "ElementType" -> "Real64"|>]}}]|>|>];
ResourceFunction["MapAtColumns"][
 "b" -> Function[ColumnwiseThread[Accumulate[#]]], tab]
Out[11]=

Accumulate multiple columns:

In[12]:=
ResourceFunction["MapAtColumns"][
 Function[ColumnwiseThread[Accumulate[#]]], tab]
Out[12]=

Start with a tabular object:

In[13]:=
tab = 
Tabular[<|"RawSchema" -> <|"ColumnProperties" -> <|"a" -> <|"ElementType" -> "Real64"|>, "b" -> <|"ElementType" -> "Real64"|>|>, "KeyColumns" -> None, "Backend" -> "WolframKernel"|>, "BackendData" -> <|"ColumnData" -> DataStructure["ColumnTable", {{
TabularColumn[<|"Data" -> {{2., 1., 3.5}, {}, None}, "ElementType" -> "Real64"|>], 
TabularColumn[<|"Data" -> {{2.78, 3.14, 1.68}, {}, None}, "ElementType" -> "Real64"|>]}}]|>|>];

MapAtColumns works with other column-based data as well:

In[14]:=
ResourceFunction["MapAtColumns"][{"a" -> Sqrt/*N, "b" -> Ceiling}, Normal@tab]
Out[14]=
In[15]:=
ResourceFunction["MapAtColumns"][{"a" -> Sqrt/*N, "b" -> Ceiling}, Dataset@tab]
Out[15]=

Properties and Relations (2) 

MapAtColumns takes functions that do not refer to column names:

In[16]:=
tab = 
Tabular[<|"RawSchema" -> <|"ColumnProperties" -> <|"a" -> <|"ElementType" -> "Real64"|>, "b" -> <|"ElementType" -> "Real64"|>|>, "KeyColumns" -> None, "Backend" -> "WolframKernel"|>, "BackendData" -> <|"ColumnData" -> DataStructure["ColumnTable", {{
TabularColumn[<|"Data" -> {{2., 1., 3.5}, {}, None}, "ElementType" -> "Real64"|>], 
TabularColumn[<|"Data" -> {{2.78, 3.14, 1.68}, {}, None}, "ElementType" -> "Real64"|>]}}]|>|>];
ResourceFunction["MapAtColumns"]["b" -> Ceiling, tab]
Out[17]=

TransformColumns can do the same thing, but needs a function that explicitly refers to the source column of the data:

In[18]:=
TransformColumns[tab, "b" -> (Ceiling[#b] &)]
Out[18]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 24 July 2025

Related Resources

License Information