Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Transform simultaneously all columns of a tabular or a selection of them
ResourceFunction["TransformTabular"][tab,f] transforms all columns of a tabular through a single function. | |
ResourceFunction["TransformTabular"][tab,f,i;;j] transforms a span of columns of a tabular through a single function. | |
ResourceFunction["TransformTabular"][tab,f,{col1,col2,…}] transforms a list of columns of a tabular through a single function. | |
ResourceFunction["TransformTabular"][f,{col1,col2,…}] represents the operator form of TransformTabular. |
Given a simple tabular with many columns:
In[1]:= | ![]() |
Out[1]= | ![]() |
We can simultaneously apply a mathematical function to all its elements:
In[2]:= | ![]() |
Out[2]= | ![]() |
Or change the colors of all cells with a given property:
In[3]:= | ![]() |
Out[3]= | ![]() |
Suppose some data science work produced errors in the final Tabular (as many Round[Missing[]] cells):
In[4]:= | ![]() |
To clean it, we can just apply replacement function to all its columns:
In[5]:= | ![]() |
Out[5]= | ![]() |
We can also specify a group of columns (as a Span or List) on which to apply a numeric operation (in the present case, we should exclude the Municipality column because it is made of strings):
In[6]:= | ![]() |
Out[6]= | ![]() |
Now on the cleaned table we can also perform an operation with ColumnwiseValue:
In[7]:= | ![]() |
Out[7]= | ![]() |
Or with ColumnwiseThread:
In[8]:= | ![]() |
Out[8]= | ![]() |
The columns specification can also be given as a list of column names of column indexes:
In[9]:= | ![]() |
Out[9]= | ![]() |
In[10]:= | ![]() |
Out[10]= | ![]() |
TransformTabular[tab,f] is equivalent to TransformColumns with multiple function specifications. Define a Tabular:
In[11]:= | ![]() |
Out[11]= | ![]() |
We can apply a transformation for each column with TransformColumns:
In[12]:= | ![]() |
Out[12]= | ![]() |
Or apply it a single time with TransformTabular[f,tab]:
In[13]:= | ![]() |
Out[13]= | ![]() |
Check that the two results are equal:
In[14]:= | ![]() |
Out[14]= | ![]() |
When the function does not involve ColumnwiseValue or ColumnwiseThread, TransformTabular[tab,f] is equivalent to Query (with Normal):
In[15]:= | ![]() |
Out[15]= | ![]() |
In[16]:= | ![]() |
Out[16]= | ![]() |
Check that the two results are equal:
In[17]:= | ![]() |
Out[17]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License