Markdown Toolbox Logo Markdown Toolbox
Home
Blog

Converting Pandas DataFrame to Markdown Table

2024-02-21

To convert a Pandas DataFrame to a Markdown table, use the to_markdown() method.

Short version

import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
print(df.to_markdown())

import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) print(df.to_markdown())

Long version

Pandas is a powerful tool for data analysis in Python and it also includes functionality for outputting data frames directly in different formats, including Markdown. When documenting your data analysis or findings, converting your DataFrame into a Markdown table can be very useful for presenting your data clearly in reports.

  1. Ensure you have the latest Pandas library where to_markdown() is available.
  2. Create your DataFrame using pd.DataFrame().
  3. Call the to_markdown() method on your DataFrame object to get the Markdown table format.

This method is straightforward and efficient for converting data for presentation or documentation purposes.