Help:Tables and locations

This page documents various ways to use a table to display information related to physical locations (e.g. countries or US states).

2 or 3-letter country codes to full names
Some source data tables only use the codes. Wrap them in brackets  to create full-name country links. To do so click on the wikitext source editing link. Click on "Advanced" in the editing toolbar. Then click on the search and replace icon on the right. Put a check in the box called "Treat search string as a regular expression." Fill in the "Search for" box with:

Make sure there is no space on the end.

Fill in the "replace with" box with:

Save the page. Copy it to Excel2Wiki to strip all the templates out, and keep the full names. Copy to a sandbox. Or skip excel2wiki and copy to a spreadsheet such as LibreOffice Calc: Edit menu > paste special > paste unformatted text. Alphabetize by the full names: Data menu > Sort ascending. This is necessary because alphabetization of codes is different from full names. Copy the table back to a sandbox via VE.

There is another way to convert the codes to full names. After putting the table in a sandbox use VE to copy just the code column as previously described at. Copy that list to a converter such as this one. Since it is a column list pick "new-line separated" from the first dropdown menu. And be sure to pick the correct direction in the second dropdown menu. Since the converted column is going to be pasted back into the table with the exact same order and length pick "include invalid", "original order", and "capitalize country names". Copy the list of results and put it into a separate one-column table as described in the previous section. Then copy that column and use it to replace the code column as described here:. The table will need to be alphabetized again since the codes alphabetize differently versus the full names.

Convert US state abbreviations to full names
If you have a list of all 50 US state abbreviations, then you can copy the full names from the show/hide boxes below. See Help:Table/Advanced. Add or remove the District of Columbia (D.C.) as necessary. Make sure the two lists are in identical order with the same number of rows. Be sure the lists are in matching alphabetical order (whether by abbreviations or full names). You can work in your sandbox. Open both tables below to see highlighted differences in alphabetization. In the end the full names will need to be in alphabetical order. See: Help:Table/Advanced.

Example: You may have a sandbox or spreadsheet of state data using state abbreviations. Open this section for editing via the visual editor (VE). Delete the D.C. row if necessary, but do not save the changes. Select and copy the full state names. You can usually paste them directly into the sandbox or spreadsheet over the abbreviations. Use with the visual editor in the sandbox. Then alphabetize the full names manually or in a spreadsheet. If spreadsheet, copy it directly to VE. If that doesn't work, copy to Excel2wiki first.

Some state lists will include the 5 permanently inhabited US territories: AS = American Samoa. GU = Guam. MP = Northern Mariana Islands. PR = Puerto Rico. VI = U.S. Virgin Islands. You will have to remove them temporarily. Or fill in their names first, and paste the other full names around them.

Horizontal table of contents for a table of states or countries
See Template:Flatlist. Example below with all 50 states (no Washington, DC). It can be added.

A flat list (without all 50 states) is in the article linked below. The list is in the map caption:
 * List of temples in the United States (LDS Church)

Clicking the state on the map there will also work. Map does not have to be there, but it is convenient. The flat list and map there are using internal links to the section links within the table.

Another method is the id= anchor method used at the link below, and discussed in the next section.
 * Legality of cannabis by U.S. jurisdiction

It is also possible to put the map and list next to each other. And they can wrap. Narrow browser window to see. '''Table of contents. Click states on map, or in list.'''

Section link or map link to a row anchor
To enable a section link 's anchor (or a map link's anchor), referencing a specific row within a table, an  parameter needs to be added to the row start   or  :

Note that each section link anchor name must be different from every other in the page (this includes heading names), to create valid XHTML and allow proper linking.


 * Example of a map link to a row

When a country label, containing a link, is clicked on the map (the link coded, for example, as  that references the anchor (within the table), coded as  ) the page scrolls so that the selected row of the table is at, or within a few lines, of the top of the browser view. Here, we use the template family Image label begin, Image label, and Image label end to lay out such a table for us:

Glossary flat list for table header abbreviations
Glossary is in alphabetical order for easier lookup. And it wraps as the screen narrows. Bei (Beijing)

Ber (Berlin)

Boc (Boca Raton)

Can (Canada)

Cha (Charleston)

Cin (Cincinnati)

Doh (Doha)

Dub (Dubai)

Ind (Indian Wells)

Mad (Madrid)

Mia (Miami)

Mos (Moscow)

Phi (Philadelphia)

Rom (Rome)

San (San Diego)

Tok (Tokyo)

Wuh (Wuhan)

Zur (Zürich)

Wikitext for glossary flat list for table header abbreviations:

Add flags. Link countries, states, etc. in tables

 * This is fast. It works with compact or long table wikitext. Single or double bars between cells.
 * Note: User:PrimeHunter (Talk) provided the code. Ask him for help, or ask at WP:VPT.
 * Note: In editing preferences check the box: "enable the editing toolbar. This is sometimes called the '2010 wikitext editor'."

Make sure the countries, states, provinces, or cities, etc. are in the first column. The regular expression wraps all the text in the first paragraph in the first cell of each row with the template. So do this before adding any styling to the first column. If there is other info besides the location name in those first cells, separate it with a blank line. See example table.

Click on the wikitext source editing link. Click on "Advanced" in the editing toolbar. Then click on the search and replace icon on the right. Put a check in the box called "Treat search string as a regular expression." Fill in the "Search for" box with:

Make sure there is no space on the end.

Fill in the "replace with" box with:

Other flag template names can be used too. If you want links without flags replace with:

Then click "Replace all". Nearly all countries, states, etc. will be linked.

Create links without flags first. If there are red links create redirects. This will also take care of all the red links in the flag lists. Then create another table with flag links. If there are any country/state links without flags open the whole page in wikitext source mode. Go to the bottom of the page and look for red-linked country data templates. Redirect them to the correct country data templates. In the edit summary of such redirects, add something like this: "Please do not delete this redirect. It is sometimes used in updates of NAME OF ARTICLE WITH TABLE." The country data template redirects may show up temporarily for a day or two here: And the corresponding flags may take a day or two to show up. Be patient. Template redirects can take time to come into effect. After that happens you may need to correct the alphabetization of a few countries.
 * Database reports/Transclusions of non-existent templates

If it is a US state list, change 'Georgia' to 'Georgia (U.S. state)'. Otherwise 'Georgia' will be treated as the country by the flag templates. Use this:

flaglist does not work with abbreviated country or state names. But if you intend to use a specialized article name (as in the next section) then it is OK to use abbreviated country or state names in this section, and then go on to the next section.

Adding specialized country/state links
For example:
 * List of U.S. states and territories by incarceration and correctional supervision rate
 * List of countries by incarceration rate

Once you have a table with flaglist, and/or other flag template such as flag, it is easy to convert the links to specialized links. So instead of Oklahoma you would have Incarceration in Oklahoma, or if that article does not exist, then Crime in Oklahoma.

Following the principle of least astonishment articles with specialized country and state links (and so on) should indicate just before the table what those links will be. This is especially important where some links have asterisks for the specialized links, meaning the other links are just standard links. See examples in the previous list links, and in the list links in the following sections.

The following works with full country or US state names. Also with abbreviated (3-letter) country names, and abbreviated (2-letter) US state names.

Use a global find-and-replace as previously described, but without regular expressions. Replace:


 * or

with

The bar after flag or flaglist is necessary to avoid problems in case you have to replace 2 flag templates.

The flagg template looks for the articles. If it finds one of the two choices it adds the link and an asterisk after the location name. If it does not find either article, it just adds the standard link.

Substitute your preferred topics in the pref= and pref2= spots.

usually left aligns the text of that column (due to the "t" at the end of the code). For some unknown reason that is not always true. In that case each cell in that column will need  in it. Or use the whole column code described here: and here: Table alignment. See example table.

Washington, D.C. - Have to force the full name to be shown:

This particular use of flagg parameters is expensive and can only be used on a few hundred links per article. As a rule of thumb, only use one big country table per article. Other country tables in the same article should use standard links.

In List of countries by incarceration rate the flagg template creates a link to Incarceration in United States. That link is a redirect to Incarceration in the United States. Add "the" as necessary to create redirect pages. Bots and admins will rapidly delete redirects that are not linked from within Wikipedia. So if you need to create redirects be sure to use them right away in an article table. Or keep them in a sandbox, or the article talk page, so that they are in use even if removed later during table changes. See examples. See list.

Flagg removes the names of the countries behind territories, subnational areas, etc.. This makes for more compact tables. Other flag templates (such as flaglist) do not do that. But that template can not create specialized country links.

See Global Search at Toolforge. Search for "{{flagg|us*eft" - in quotes. To get transclusion count and list of articles.

The flagg template does not work if colspan is used before the country/state name. {{tl|flag+link}} works. See: Template talk:Flagg.

Without asterisks after location names
When all the locations in a table list have specialized articles, then there is no need for the asterisks to distinguish those specialized location articles.

Replace  with   to remove the asterisks, and lower the expensive server load.

See Global Search at Toolforge. Search for "{{flagg|uspeft" - in quotes. To get transclusion count and list of articles.

Without asterisks. Only one preference
See this version of List of countries by hospital beds.

Replace:

with

Convert rows to columns and columns to rows
Sometimes there is a need to transpose columns and rows (move rows to columns, and columns to rows). For simple tables, this can be done via the "transpose rows and columns" function of Copy & Paste Excel-to-Wiki, or via the "transpose" feature of a third-party spreadsheet program such as Microsoft Excel, the free web-based Google Sheets, or the free downloadable software LibreOffice Calc.

To transpose the table with a third-party spreadsheet program, copy the published table on the Wikipedia page and paste it into a new blank document in your spreadsheet program. While the pasted cells are still selected in the spreadsheet, copy them again by right-clicking and choosing "Copy" from the context menu. Open a new blank spreadsheet, click in the upper-left cell, right click on it, and choose "Paste Special". In Microsoft Excel, check the "Transpose" box at the bottom of the dialogue and hit Okay. In Google Sheets, choose "Transpose" from the sub-menu. in LibreOffice Calc, choose "Transpose" from the sub-menu. Perform any required editing of the transposed table, and copy the new table directly from the spreadsheet program into visual editor, or into Excel2Wiki.

For more complicated operations, such as consolidating multiple rows with the same header into a single column, you can use the "pivot table" feature of an external spreadsheet program. For example; the data for the overdose rates table by state for United States drug overdose death rates and totals over time comes from a csv file and is converted to wikitable format via one of the previously mentioned csv converters. The year headers in the left table below need to become the column headers in the right table.

To re-arrange the table using pivot tables, copy and paste the table into a spreadsheet program such as freeware LibreOffice Calc.

In LibreOffice Calc, first make sure the headers are in the top row, and that they are not sticky (frozen). Then select all from the view menu. Then click on the "Pivot Table" command from the Insert menu. Click OK in the popup box. In the next dialog box drag "Year" to the "Column Fields" box, and drag "State" to the "Row Fields" box. Drag "Rate" to the "Data Fields" box. In the options menu decide whether you want the rows or columns totaled. Click OK. The table will convert to the new format with the years as column headers. To avoid problems copy the table to a new sheet before further editing. See: "Only Copy Visible Cells" in Calc help. For more help see: LibreOffice: Pivot Tables and LibreOffice Help: Pivot Table. If necessary, convert state or country abbreviations to full names.

When done editing, copy the new table (if small) directly from the Calc page into visual editor, or into Excel2Wiki first, and then into VE. Then use VE to delete the summation column and row if necessary.

Pick selected dates from massive .csv files
COVID-19 pandemic deaths has a few tables by year of cumulative deaths by country on the first of each month. The source is a massive WHO (World Health Organization) csv file. Download here. You could convert it to a massive wikitable as described elsewhere, and delete the hundreds of unwanted date columns over several hours.

Or you could open the csv file in a spreadsheet such as freeware LibreOffice Calc. Then delete all columns except for "Country", "Date reported", and "Cumulative deaths" columns (select, right-click column head, delete). Save as .ods file. Then use the autofilter function to select just the dates of interest from a checklist. Click anywhere in the table. Then: Data menu > AutoFilter. Dropdown menus will show up on all column heads. In the "Date reported" dropdown menu clear the "All" box by clicking it. This unchecks all the dates. Then check the dates you want. In this case all the first of the month dates for the desired year. Click OK. Save the file. See video. See: "Applying AutoFilter" in Calc help.

In order to completely remove all the unwanted data you must copy and paste that table to a new Calc ods file. A simple paste works (edit menu > paste). This greatly reduces the file size. See: "Only Copy Visible Cells" in Calc help.

On this smaller file use the "Pivot Table" method described in the previous section to put the dates as column heads. Select all from the edit menu. Then click on the "Pivot Table" command from the Insert menu. Click OK in the popup box. In the next dialog box drag "Date reported" to the "Column Fields" box, and drag "Country" to the "Row Fields" box. Drag "Cumulative deaths" to the "Data Fields" box. Click OK. Copy and paste that table to a new Calc ods file to prevent Pivot complications, and to do further editing.

Add thousands separator as needed. See: Help:Creating tables. Sort latest month. Save it. Then copy that Calc table to Excel2Wiki, or directly to the VisualEditor (if small). For more info go here.

This method can be used for creating or updating many Wikipedia country tables that need only the latest data, or selected dates, from large multi-year data file sources. Just scrolling back and forth through one huge data file could take hours otherwise.

Data from latest available year
List of countries by firearm-related death rate has both a yearly rate table, and a rate table showing only the latest available year. The latest-year format allows for easier sorting, ranking, and comparison of countries. Due to the limits of screen width the yearly table is missing some countries because their latest available year is too far back in time. For latest-year instructions see:
 * Talk:List of countries by firearm-related death rate/LibreOffice Calc instructions for latest available year

List of countries by intentional homicide rate has a table that only shows data for the latest available year. It has 2 data columns (counts and rates). See updating instructions:
 * Talk:List of countries by intentional homicide rate/LibreOffice Calc instructions
 * Talk:List of countries by intentional homicide rate/Excel instructions

Separate counts and rates to 2 columns
Rates are per 100,000 inhabitants.

See List of countries by intentional homicide rate. Its reference dataset comes with counts and rates alternating in the same column. See example here. That example is after only the latest available year is showing in the spreadsheet. See previous section above for links to detailed instructions.

In LibreOffice Calc select the column head for the column containing the word "counts". Then: Data menu > More Filters > Standard Filter > Filter Criteria. Enter "Counts" in Value spot. Click OK. Only rows with "Counts" in them will be kept. Delete column that only contains the word "Counts". Copy and paste to new Calc file to get accurate row numbering, and small file size. Note the number of rows.

Start over and do the same except for "Rates." Hopefully, the number of rows are the same as for the counts sheet. You now have 2 sheets. One for counts and one for rates. Copy the counts column to the rates sheet.

Automated tables updated daily by bots
Some automated tables: The next 2 templates are almost the same:
 * Template:COVID-19 pandemic data. Scrolling. Used here: Portal:COVID-19.
 * Template:COVID-19 vaccination data. Scrolling. Used here:
 * Deployment of COVID-19 vaccines
 * Template:COVID-19 pandemic death rates. Scrolling. Used here:
 * COVID-19 pandemic by country and territory
 * Template:COVID-19 pandemic death rates by country. Scrolling. Used here:
 * COVID-19 pandemic death rates by country.