Skip to content

Instantly share code, notes, and snippets.

@jplsightm
Last active January 31, 2023 07:32
Show Gist options
  • Save jplsightm/c7df5cd2bc62dc84c5158a80cf0af6df to your computer and use it in GitHub Desktop.
Save jplsightm/c7df5cd2bc62dc84c5158a80cf0af6df to your computer and use it in GitHub Desktop.
Convert a Pandas Dataframe to Markdown
import pandas as pd
from tabulate import tabulate
def pandas_df_to_markdown_table(df):
# Dependent upon ipython
# shamelessly stolen from https://stackoverflow.com/questions/33181846/programmatically-convert-pandas-dataframe-to-markdown-table
from IPython.display import Markdown, display
fmt = ['---' for i in range(len(df.columns))]
df_fmt = pd.DataFrame([fmt], columns=df.columns)
df_formatted = pd.concat([df_fmt, df])
#display(Markdown(df_formatted.to_csv(sep="|", index=False)))
return Markdown(df_formatted.to_csv(sep="|", index=False))
# return df_formatted
def df_to_markdown(df, y_index=False):
blob = tabulate(df, headers='keys', tablefmt='pipe')
if not y_index:
# Remove the index with some creative splicing and iteration
return '\n'.join(['| {}'.format(row.split('|', 2)[-1]) for row in blob.split('\n')])
return blob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment