Help:Tables and locations
- Help:Table (main page)
- Introduction
- VisualEditor
- Basics
- Creation
- Locations
- Advanced
- Category
- MOS (Accessibility).
This help page is a how-to guide. It explains concepts or processes used by the Wikipedia community. It is not one of Wikipedia's policies or guidelines, and may reflect varying levels of consensus. |
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
[edit]Some source data tables only use the codes. Wrap them in brackets {{ABC}}
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:
(\|-.*\n\|\s*)([^\|\n]*)
Make sure there is no space on the end.
Fill in the "replace with" box with:
$1{{$2}}
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 column from one table to another. 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: §§ Help:Tables and Copy column from one table to another. The table will need to be alphabetized again since the codes alphabetize differently versus the full names.
Convert US state abbreviations to full names
[edit]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#Copy column to table. 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#Sort. Spreadsheet & VE.
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 (Ctrl+C) the full state names. You can usually paste them directly into the sandbox or spreadsheet over the abbreviations. Use Ctrl+V 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.
Full name | 2-letter USPS |
---|---|
Alabama | AL |
Alaska | AK |
Arizona | AZ |
Arkansas | AR |
California | CA |
Colorado | CO |
Connecticut | CT |
Delaware | DE |
District of Columbia | DC |
Florida | FL |
Georgia | GA |
Hawaii | HI |
Idaho | ID |
Illinois | IL |
Indiana | IN |
Iowa | IA |
Kansas | KS |
Kentucky | KY |
Louisiana | LA |
Maine | ME |
Maryland | MD |
Massachusetts | MA |
Michigan | MI |
Minnesota | MN |
Mississippi | MS |
Missouri | MO |
Montana | MT |
Nebraska | NE |
Nevada | NV |
New Hampshire | NH |
New Jersey | NJ |
New Mexico | NM |
New York | NY |
North Carolina | NC |
North Dakota | ND |
Ohio | OH |
Oklahoma | OK |
Oregon | OR |
Pennsylvania | PA |
Rhode Island | RI |
South Carolina | SC |
South Dakota | SD |
Tennessee | TN |
Texas | TX |
Utah | UT |
Vermont | VT |
Virginia | VA |
Washington | WA |
West Virginia | WV |
Wisconsin | WI |
Wyoming | WY |
Full name | 2-letter USPS |
---|---|
Alaska | AK |
Alabama | AL |
Arkansas | AR |
Arizona | AZ |
California | CA |
Colorado | CO |
Connecticut | CT |
District of Columbia | DC |
Delaware | DE |
Florida | FL |
Georgia | GA |
Hawaii | HI |
Iowa | IA |
Idaho | ID |
Illinois | IL |
Indiana | IN |
Kansas | KS |
Kentucky | KY |
Louisiana | LA |
Massachusetts | MA |
Maryland | MD |
Maine | ME |
Michigan | MI |
Minnesota | MN |
Missouri | MO |
Mississippi | MS |
Montana | MT |
North Carolina | NC |
North Dakota | ND |
Nebraska | NE |
New Hampshire | NH |
New Jersey | NJ |
New Mexico | NM |
Nevada | NV |
New York | NY |
Ohio | OH |
Oklahoma | OK |
Oregon | OR |
Pennsylvania | PA |
Rhode Island | RI |
South Carolina | SC |
South Dakota | SD |
Tennessee | TN |
Texas | TX |
Utah | UT |
Virginia | VA |
Vermont | VT |
Washington | WA |
Wisconsin | WI |
West Virginia | WV |
Wyoming | WY |
Horizontal table of contents for a table of states or countries
[edit]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:
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.
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
[edit]Note that this method is problematic for people using larger text sizes in their browser. A clickable image map (such as the US map in the previous section) is a better choice:
To enable a section link's anchor (or a map link's anchor), referencing a specific row within a table, an id="section link anchor name"
parameter needs to be added to the row start |-
or <tr>
:
|- id="section link anchor name"
<tr id="section link anchor name">
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 [[#New South Wales|New South Wales]]
that references the anchor (within the table), coded as |- id="New South Wales"
) 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:
State or territory |
---|
Capital Territory |
New South Wales |
Northern Territory |
Queensland |
South Australia |
Tasmania |
Victoria |
Western Australia |
{| class=wikitable
|+ [[Australia]]
! State or territory
|- id="Australian Capital Territory"
| [[Australian Capital Territory|Capital Territory]]
|- id="New South Wales"
| [[New South Wales]]
|- id="Northern Territory"
| [[Northern Territory]]
|- id="Queensland"
| [[Queensland]]
|- id="South Australia"
| [[South Australia]]
|- id="Tasmania"
| [[Tasmania]]
|- id="Victoria"
| [[Victoria (state)|Victoria]]
|- id="Western Australia"
| [[Western Australia]]
|}
Glossary flat list for table header abbreviations
[edit]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)
Player | Titles | Boc | Dub | Doh | Ind | Mia | Cha | Mad | Ber | Rom | Can | San | Cin | Phi | Mos | Tok | Wuh | Zur | Bei | Years |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Serena Williams | 23 | - | - | - | 2 | 8 | 1 | 2 | - | 4 | 3 | - | 2 | - | - | - | - | - | 1 | 1999–2016 |
Martina Hingis | 17 | - | - | - | 1 | 2 | 2 | - | 1 | 2 | 2 | - | - | - | 1 | 5 | - | 1 | - | 1997–2007 |
Wikitext for glossary flat list for table header abbreviations:
{{flatlist|
* '''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)
}}
Add flags. Link countries, states, etc. in tables
[edit]- 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 {{flaglist}}
template. {{flaglist|Country name}}
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:
(\|-.*\n\|\s*)([^\|\n]*)
Make sure there is no space on the end.
Fill in the "replace with" box with:
$1{{flaglist|$2}}
Other flag template names can be used too. If you want links without flags replace with:
$1[[$2]]
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.
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|Georgia (U.S. state)|name=Georgia}}
{{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
[edit]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:
{{flag|
- or
{{flaglist|
with
{{flagg|us*eft|pref=Incarceration in|pref2=Crime in|
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.
{{flagg|us*eft|
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 style=text-align:left
in it. Or use the whole column code described here: #Column alignment and here: {{Table alignment}}. See example table.
Washington, D.C. - Have to force the full name to be shown:
{{flagg|us*eft|pref=Crime in|Washington, D.C.|name=Washington, D.C.}}
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. {{flag+link}} works. See: Template talk:Flagg#Does not work with colspan.
Without asterisks after location names
[edit]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 {{flagg|us*eft
with {{flagg|uspeft
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
[edit]See this version of List of countries by hospital beds.
Replace:
{{flaglist
with
{{flag+link|Health in
Convert rows to columns and columns to rows
[edit]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.
Year | State | Rate |
---|---|---|
2019 | AL | 16.3 |
2019 | AK | 17.8 |
2019 | AZ | 26.8 |
2018 | AL | 16.6 |
2018 | AK | 14.6 |
2018 | AZ | 23.8 |
2017 | AL | 18 |
2017 | AK | 20.2 |
2017 | AZ | 22.2 |
State | 2017 | 2018 | 2019 |
---|---|---|---|
AL | 18 | 16.6 | 16.3 |
AK | 20.2 | 14.6 | 17.8 |
AZ | 22.2 | 23.8 | 26.8 |
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
[edit]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#Separate after 3 digits. 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
[edit]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:
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
[edit]Rates are per 100,000 inhabitants.
Location | Year | Type | Value |
---|---|---|---|
Afghanistan | 2021 | Counts | 1613 |
Afghanistan | 2021 | Rates | 4.0 |
Albania | 2021 | Counts | 66 |
Albania | 2021 | Rates | 2.3 |
Location | Year | Rates | Counts |
---|---|---|---|
Afghanistan | 2021 | 4.0 | 1613 |
Albania | 2021 | 2.3 | 66 |
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
[edit]Some automated tables:
- Template:COVID-19 pandemic data. Scrolling. Used here: Portal:COVID-19.
- Template:COVID-19 vaccination data. Scrolling. Used here:
The next 2 templates are almost the same:
- Template:COVID-19 pandemic death rates. Scrolling. Used here:
- Template:COVID-19 pandemic death rates by country. Scrolling. Used here:
See also
[edit]- Help:Tables for general information about using tables. It also has many links in its "See also" and "External links" sections. Rather than duplicating them here.