How to | Put Headings in a Table

Mathematica provides great flexibility for displaying and styling headings in a table. You can use Prepend or ArrayFlatten to add headings to rows and columns and then use Grid with any of its styling elements to display them in a table.

Mathematica lets you control and customize the display of data in your table. Headings are just like any other element of a table.

Set up a table of values:

In[49]:=
Click for copyable input
Out[49]=

Prepend headings for columns:

In[50]:=
Click for copyable input
Out[50]=

Display the data, with column headings, using Grid:

In[51]:=
Click for copyable input
Out[51]=

Use MapThread to prepend headings for rows:

In[52]:=
Click for copyable input
Out[52]=

Display the data with headings for rows and columns, using Grid with Frame:

In[53]:=
Click for copyable input
Out[53]=
    

Set up a table of circles:

In[54]:=
Click for copyable input

Use GraphicsGrid to display the circles in a grid:

In[55]:=
Click for copyable input
Out[55]=

Create and style column headings:

In[56]:=
Click for copyable input
Out[56]=

Prepend the styled column headings to the circles:

In[57]:=
Click for copyable input

View the styled table with GraphicsGrid:

In[58]:=
Click for copyable input
Out[58]=
    

The following data represents yields for three types of soils and two types of corn seeds:

In[59]:=
Click for copyable input

The data can be grouped by soil type by using First with GatherBy to gather the data by the first element of each data point:

In[85]:=
Click for copyable input
Out[85]=

To prepare the data for display in a table, use Flatten at level one, which here makes one list of triples. The symbol specifies the most recent output, which in this case is the grouped data:

In[61]:=
Click for copyable input
Out[61]=

The observation number within each soil type will serve as the corresponding row heading.

Use Length to count the number of observations within each soil type:

In[62]:=
Click for copyable input
Out[62]=

Use Range to generate a list of successive integers beginning with 1 and ending with the number of observations within each soil type:

In[63]:=
Click for copyable input
Out[63]=

Prepare the numbers for use as row headings by using Flatten to compress them into a single list:

In[64]:=
Click for copyable input
Out[64]=

Create column headings for the soil type, seed type, and yield data. You will create a column heading for the observation number in the next step, so you do not need to do so here:

In[65]:=
Click for copyable input

Use ArrayFlatten to add the observation numbers, along with the column heading , to the data:

In[87]:=
Click for copyable input
Out[87]=

Use Grid to display the data in a table:

In[88]:=
Click for copyable input
Out[88]=

Use some of the options available in Grid to add styling to the table:

In[89]:=
Click for copyable input
Out[89]=

For more examples of table formatting and styling, see "How to: Format a Table of Data", and "Grids, Rows, and Columns".

    

You can also create tables and add headings to them using TableForm.

Define a set of data to work with:

In[1]:=
Click for copyable input

Create the row and column headings for your data:

In[1]:=
Click for copyable input

Create a table with TableForm, and use the TableHeadings option to add your headings:

In[3]:=
Click for copyable input
Out[3]//TableForm=

While TableForm allows you to create tables, it does not support the extensive styling options that are available to Grid.

New to Mathematica? Find your learning path »
Have a question? Ask support »