How to | Put Headings in a Table

The Wolfram Language 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.

The Wolfram Language 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[1]:=
Click for copyable input
Out[1]=

Prepend headings for columns:

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

Display the data, with column headings, using Grid:

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

Use MapThread to prepend headings for rows:

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

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

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

Set up a table of circles:

In[1]:=
Click for copyable input

Use GraphicsGrid to display the circles in a grid:

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

Create and style column headings:

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

Prepend the styled column headings to the circles:

In[4]:=
Click for copyable input

View the styled table with GraphicsGrid:

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

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

In[1]:=
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[2]:=
Click for copyable input
Out[2]=

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[3]:=
Click for copyable input
Out[3]=

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[4]:=
Click for copyable input
Out[4]=

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

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

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

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

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[7]:=
Click for copyable input

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

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

Use Grid to display the data in a table:

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

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

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

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[2]:=
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.

Tutorials

See Also

Grid  GraphicsGrid  Prepend  Map  MapThread  ArrayFlatten  List  TableForm  TableHeadings  Style  ItemStyle  Frame  Background