Introduction
This article will walk you through the process of creating a sample dataframe using Pandas and exporting it to Markdown format with the built-in to_markdown() function.
Creating a Sample Dataframe
Before exporting a dataframe to Markdown, let's create a sample dataframe using Pandas. In this example, we will create a dataframe containing information about different fruits and their nutritional values.
import pandas as pd
data = {
'Fruit': ['Apple', 'Banana', 'Orange', 'Strawberry', 'Kiwi'],
'Calories': [95, 89, 47, 32, 61],
'Fiber': [4.4, 2.6, 2.4, 2.0, 3.0],
'Vitamin C': [14, 8.7, 53, 97, 92.7]
}
df = pd.DataFrame(data)
Exporting Dataframes to Markdown with Pandas
Pandas has a built-in function to_markdown() for exporting dataframes to Markdown format. First, make sure you have the required package installed:
$ pip install -U pandas[markdown]
Now, you can use the to_markdown() function to export your dataframe to Markdown.
markdown_table = df.to_markdown()
print(markdown_table)
The output will be a Markdown formatted table:
| | Fruit | Calories | Fiber | Vitamin C |
|---:|:-----------|-----------:|--------:|------------:|
| 0 | Apple | 95 | 4.4 | 14 |
| 1 | Banana | 89 | 2.6 | 8.7 |
| 2 | Orange | 47 | 2.4 | 53 |
| 3 | Strawberry | 32 | 2 | 97 |
| 4 | Kiwi | 61 | 3 | 92.7 |
Options of to_markdown()
The to_markdown() function in Pandas provides various options to customize the appearance of the exported Markdown table. In this section, I will explore some of these options and explain how to use them effectively.
buf
This option allows you to write the Markdown table to a file-like object or a string buffer. By default, it is set toNone, which means the function will return the resulting Markdown table as a string.
with open("output.md", "w") as file:
df.to_markdown(buf=file)
index
This boolean option, when set toTrue, includes the index column in the output Markdown table. By default, it is set toTrue. To exclude the index from the output table, set it toFalse.
markdown_table = df.to_markdown(index=False)
print(markdown_table)
header
This option allows you to include custom headers for the table columns. By default, it is set toTrue, which means the function will use the existing column names as headers. If you want to use custom headers, pass a list of strings with the new headers.
custom_headers = ['Fruit Name', 'Cal (kcal)', 'Fiber (g)', 'Vit C (mg)']
markdown_table = df.to_markdown(headers=custom_headers)
print(markdown_table)
tablefmt
This option controls the table format used in the output Markdown table. By default, it is set to'pipe'. Other available formats include'simple','grid', and'tsv'.
markdown_table = df.to_markdown(tablefmt='grid')
print(markdown_table)
floatfmt
This option allows you to specify the format for floating-point numbers. By default, it is set to '.6g'. You can provide a single string for all floating-point columns or a list of strings for each floating-point column individually.
markdown_table = df.to_markdown(floatfmt='.2f')
print(markdown_table)
numalign
This option controls the alignment of numeric columns. By default, it is set to'right'. Other available alignments include'left','center', or'right'.
markdown_table = df.to_markdown(numalign='center')
print(markdown_table)
stralign
This option controls the alignment of non-numeric columns. By default, it is set to'left'. Other available alignments include'left','center', or'right'.
markdown_table = df.to_markdown(stralign='center')
print(markdown_table)