I would like to print dataframe to markdown and use it in markdownhere.
I saw to_markdown() api but the format not work in markdownhere.
So I try below code, it works fine:
import pandas as pd
def dumpmd(df):
out = []
out.append("|".join(df.columns))
r = []
for name in df.columns:
r.append(":-")
out.append("|".join(r))
for i,row in df.iterrows():
r = []
r1 = []
for e in row:
r.append(str(e))
out.append("|".join(r))
out = "\n".join(out)
return out
data = [['1','B','C'],
['2','E','F'],
['3','H','I']]
df = pd.DataFrame(data,columns=["C1" ,"C2","C3"])
print(df.to_markdown(tablefmt='pipe',index=False))
print(dumpmd(df))
Output:
| C1 | C2 | C3 |
|----+----+----|
| 1 | B | C |
| 2 | E | F |
| 3 | H | I |
C1|C2|C3
:-|:-|:-
1|B|C
2|E|F
3|H|I
Would anyone help simplify this code or any better solution to output expected markdown table from dataframe?
I proposed you three methods to do this :
(My preference is to_markdown method)
Source dataframe
import pandas as pd
data = [['1','B','C'],
['2','E','F'],
['3','H','I']]
df = pd.DataFrame(data,columns=["C1" ,"C2","C3"])
Method 1 : using a specialized function
Could be done the hard way with a writing function :
def dumpmd(df):
out = ["|" + "|".join(df.columns) + "|"]
out.append("|" + "|".join([":-" for _ in df.columns]) + "|")
out.extend("|" + "|".join(map(str, row)) + "|" for _, row in df.iterrows())
return "\n".join(out)
print(dumpmd(df))
|C1|C2|C3|
|:-|:-|:-|
|1|B|C|
|2|E|F|
|3|H|I|
Method 2 : Pandas markdown method
Use to_markdown()
Pandas function directly, specifying the appropriate array format for Markdown.
df.to_markdown(tablefmt='pipe')
'| | C1 | C2 | C3 |\n|---:|-----:|:-----|:-----|\n| 0 | 1 | B | C |\n| 1 | 2 | E | F |\n| 2 | 3 | H | I |'
Another interesting tablefmt
parameter is grid
for instance for rendering :
df.to_markdown(tablefmt='grid')
'+----+------+------+------+\n| | C1 | C2 | C3 |\n+====+======+======+======+\n| 0 | 1 | B | C |\n+----+------+------+------+\n| 1 | 2 | E | F |\n+----+------+------+------+\n| 2 | 3 | H | I |\n+----+------+------+------+'
cool grid rendering :
+----+------+------+------+
| | C1 | C2 | C3 |
+====+======+======+======+
| 0 | 1 | B | C |
+----+------+------+------+
| 1 | 2 | E | F |
+----+------+------+------+
| 2 | 3 | H | I |
+----+------+------+------+
Method 3 : tabulate library
Use the tabulate
library to convert the DataFrame to a tabulated string, then convert it to Markdown.
from tabulate import tabulate
# Convert the DataFrame to a tabbed string
tabular_data = tabulate(df, headers='keys', tablefmt='pipe')
# Add headers and footers to the table
markdown_data = f"| {' '.join(df.columns)} |\n| {'-' * len(' '.join(df.columns))} |\n{tabular_data}\n"
print(markdown_data)
'| C1 C2 C3 |\n| -------- |\n| | C1 | C2 | C3 |\n|---:|-----:|:-----|:-----|\n| 0 | 1 | B | C |\n| 1 | 2 | E | F |\n| 2 | 3 | H | I |\n'