python-tabulate icon indicating copy to clipboard operation
python-tabulate copied to clipboard

(bug) : pandas.Dataframe columns.name is not displayed

Open LeG2 opened this issue 2 years ago • 1 comments

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)

LeG2 avatar Apr 03 '23 07:04 LeG2

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")

ludwig-liveeo avatar Sep 25 '24 09:09 ludwig-liveeo