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.
m = Table[i ^ 2 - j ^ 3, {i, 3}, {j, 4}]TableOfValues1 = Prepend[m, {"First", "Second", "Third", "Fourth"}]Display the data, with column headings, using Grid:
Grid[TableOfValues1]Use MapThread to prepend headings for rows:
TableOfValues2 = MapThread[Prepend, {TableOfValues1, {"", "1st", "2nd", "3rd"}}]Display the data with headings for rows and columns, using Grid with Frame:
Grid[TableOfValues2, Frame -> All]n = Table[Graphics[Circle[], AspectRatio -> RandomReal[]], {3}, {5}];Use GraphicsGrid to display the circles in a grid:
GraphicsGrid[n, Frame -> All]Create and style column headings:
headings = Style[#, {Orange, Bold, 12}]& /@ {"One", "Two", "Three", "Four", "Five"}Prepend the styled column headings to the circles:
TableOfCircles = Prepend[n, headings];View the styled table with GraphicsGrid:
GraphicsGrid[TableOfCircles, Frame -> All]The following data represents yields for three types of soils and two types of corn seeds:
agdata1 = {{clay, seedB, 175}, {silty, seedB, 180}, {clay, seedB, 165}, {sandy, seedA, 168}, {clay, seedA, 184}, {sandy, seedB, 171}, {sandy, seedB, 173}, {sandy, seedA, 189}, {clay, seedA, 186}, {silty, seedB, 174}, {clay, seedA, 192}, {clay, seedA, 184}, {clay, seedA, 179}, {sandy, seedA, 182}, {sandy, seedB, 177}, {silty, seedA, 180}, {clay, seedB, 175}, {silty, seedB, 181}, {sandy, seedA, 176}, {silty, seedB, 190}};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:
GatherBy[agdata1, First]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:
agdata2 = Flatten[%, 1]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:
Length /@ GatherBy[agdata1, First]Use Range to generate a list of successive integers beginning with 1 and ending with the number of observations within each soil type:
Range[%]Prepare the numbers for use as row headings by using Flatten to compress them into a single list:
rowHeadings1 = Flatten[%]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:
colHeadings1 = {"Soil Type", "Seed Type", "Yield"};Use ArrayFlatten to add the observation numbers, along with the column heading "Observation", to the data:
agdata3 = ArrayFlatten[{{{{"Observation"}}, {colHeadings1}}, {List /@ rowHeadings1, agdata2}}]Use Grid to display the data in a table:
Grid[agdata3]Use some of the options available in Grid to add styling to the table:
Grid[agdata3, ItemStyle -> {Automatic, {1 -> {Bold, 14}}}, Frame -> {All, 1 -> True}, Background -> {None, {{LightOrange, LightGray}}}]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:
d = {{5, 7}, {4, 2}, {10, 3}};Create the row and column headings for your data:
rowHeadings2 = {"Group A", "Group B", "Group C"};
colHeadings2 = {"y1", "y2"};Create a table with TableForm, and use the TableHeadings option to add your headings:
TableForm[d, TableHeadings -> {rowHeadings2, colHeadings2}]While TableForm allows you to create tables, it does not support the extensive styling options that are available to Grid.