Tables in Markdown (in Jupyter)

MarkdownJupyter

Markdown Problem Overview


A super basic question: why is the following not rendering in Markdown - which happens to be in a jupyter notebook

Raw code

### Results

| --- | --- | --- |
| Stretch/Untouched | ProbDistribution | Accuracy |
| --- | --- | --- |
| Stretched | Gaussian | .843 |

Code as it looks in jupyter in edit mode

enter image description here

Rendering in jupyter

enter image description here

So the table did not render properly

Update I did some twiddling and now it renders.. but still uncertain why the original code did not work

enter image description here

Markdown Solutions


Solution 1 - Markdown

The first row of the table defines the headers, then the next row defines the alignment of each column. You duplicated the alignment at the top of the table and where it's actually supposed to go.

The right Markdown should simply be what you have in your syntax, but remove the first row:

| Stretch/Untouched | ProbDistribution | Accuracy |
| --- | --- | --- |
| Stretched | Gaussian | .843 |

The --- in between the column definitions | | mean that the column is unjustified. In standard Markdown, this would align to the left of the column but in Jupyter notebook, it appears to align to the right instead.

With that, I get this table:

enter image description here


If you'd like to left align or centre align, you can use :- and :-: respectively. Depending on what Jupyter notebook environment you're using, you will need to use -: to right align.

| Stretch/Untouched | ProbDistribution | Accuracy |
| :- | -: | :-: |
| Stretched | Gaussian | .843

The first column will be left-aligned, the centre column is right-aligned and the last column is centre aligned. Interestingly using Google Colab, --- left aligns the text:

enter image description here


Is the alignment not working as expected in your Jupyter notebook?

This section is now outdated - the alignment should work as of this date (February 9th, 2022). See the edit below.

The alignment syntax that I've mentioned above, unfortunately, does not work as of this date (June 25th, 2020) when using local installations of the Jupyter notebook environment. This is because of a bug in the Jupyter source where the Markdown alignment is not taken into account and all of the text is right-aligned. See the Github issue here: https://github.com/jupyter/notebook/issues/3919. However, it does work using jupyterlab as well as on Google Colab.


Edit - February 9th, 2022

Jupyter notebook versions from 6.0.0 onwards should contain the fix. If the alignment isn't working, please ensure that you upgrade your version of Jupyter notebook and try again.

pip install --upgrade notebook

Solution 2 - Markdown

Even though this question has been answered, still dropping this here - it may help someone else. I too was not able to render tables in jupyter notebook.

Example:


    | | Sentence #  | Word    | POS   | Tag   |
|---:|:-------------|:-----------|:------|:------|
| 1 | Sentence: 1  | They       | PRP   | O     |
| 2 | Sentence: 1  | marched    | VBD   | O     |

Output:

| | Sentence # | Word | POS | Tag | |---:|:-------------|:-----------|:------|:------| | 1 | Sentence: 1 | They | PRP | O | | 2 | Sentence: 1 | marched | VBD | O |

I was not able to figure out why this was happening but for some strange reason when I enter text in the first cell of first row, it was rendering fine. So here I entered Sno. in the very first cell and it's working fine.

Output:

enter image description here

Solution 3 - Markdown

One could also use HTML tags in a notebook Markdown to create the table:

<table ><tr><th >Stretch/Untouched <th><th> ProbDistribution <th><th> Accuracy <tr><tr>
<tr><td> Stretched <td><td> Gaussian <td><td> .843 <td><tr><table>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWestCoastProjectsView Question on Stackoverflow
Solution 1 - MarkdownrayryengView Answer on Stackoverflow
Solution 2 - MarkdownNutanView Answer on Stackoverflow
Solution 3 - MarkdownCoralReefView Answer on Stackoverflow