SIH Tech Tidbits

Useful tips, libraries and tools from the Sydney Informatics Hub team

Pandas dataframe to Markdown


The Problem

In building up code and tracking the bugs, it is extremely useful posting the traceback in a GitHub issue. The problem is that when we are dealing with pandas dataframes, it is not easy to convert the df into a markdown. Given the following df:

import pandas as pd

df = pd.DataFrame([['a',1],['b',2],['c',3]], columns=['letters', 'numbers'])

You need to incorporate it into something like this:

```python
  letters  numbers
0       a        1
1       b        2
2       c        3
```

to have the following output:

  letters  numbers
0       a        1
1       b        2
2       c        3

The solution

Starting from version 1.0 Pandas now inlude a to_markdown() method that would make the things easier for us. See below:

>> print(df.to_markdown())
|    | letters   |   numbers |
|---:|:----------|----------:|
|  0 | a         |         1 |
|  1 | b         |         2 |
|  2 | c         |         3 |

Then copy and paste it in a GitHub Issue:

Formatted table displayed in a GitHub issue