python-tabulate
python-tabulate copied to clipboard
(bug) : pandas.Dataframe columns.name is not displayed
pandas DataFrame columns.name (top-left cell when indexes are displayed) is missing
from tabulate import tabulate
from pandas import DataFrame
df = DataFrame([[1,2],[3,4]],columns=['a','b'])
df.columns.name = 'some_name'
print(df)
# let's see DataFrame :
# some_name a b
# 0 1 2
# 1 3 4
tabulate(df, tablefmt='pipe', headers='keys', showindex=True)
# | | a | b |\n|---:|----:|----:|\n| 0 | 1 | 2 |\n| 1 | 3 | 4 |
# NB : 'some_name' missing at top-left corner
| a | b | |
|---|---|---|
| 0 | 1 | 2 |
| 1 | 3 | 4 |
should display
| some_name | a | b |
|---|---|---|
| 0 | 1 | 2 |
| 1 | 3 | 4 |
(tested with tabulate 0.9.0, python 3.10)
This is how I work around this issue:
raw_table = [row.split(",") for row in df.to_csv(header=True, index_label=df.index[0]).split("\n") if row != ""]
markdown_table = tabulate(tabular_data=raw_table[1:], headers=raw_table[0], tablefmt="pipe")