Simple Markdown Table Output From pandas DataFrames

In passing, I note that we can easily generate simple markdown style table output from a pandas data frame using the pandas .to_markdown() dataframe method:

Here’s the associated code fragment:

data = """colA, colB, colC
this, that, 1
or, another, 2"""

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO(data))
md_table = df.to_markdown(index=False)

print(md_table)

I also note that Stack Overflow recently (ish!) added support for markdown tables (announcement) using Github-flavoured markdown syntax.

Author: Tony Hirst

I'm a Senior Lecturer at The Open University, with an interest in #opendata policy and practice, as well as general web tinkering...

%d bloggers like this: