pythonpandastabulatepython-tabulate

to_markdown doesn't pass intfmt to tabulate


Comma separation seems to work fine without a Pandas DataFrame. Executing

from tabulate import tabulate
print(tabulate([['2023m06',2000],['2023m05',100000]], 
               tablefmt='rounded_grid', 
               intfmt=','))

gives me

╭─────────┬─────────╮
│ 2023m06 │   2,000 │
├─────────┼─────────┤
│ 2023m05 │ 100,000 │
╰─────────┴─────────╯

as expected. But when I attempt this with a Pandas DataFrame

import pandas as pd
test = {'2023m06': [2000], '2023m05': [100000]}
df = pd.DataFrame.from_dict(test,
                            orient='index',
                            columns=['myColumn'])
print(df.to_markdown(tablefmt='rounded_grid',
                     intfmt=','))

my result

╭─────────┬────────────╮
│         │   myColumn │
├─────────┼────────────┤
│ 2023m06 │       2000 │
├─────────┼────────────┤
│ 2023m05 │     100000 │
╰─────────┴────────────╯

does not have any commas. Can anyone see what I'm doing wrong?


Solution

  • I don't know why, but if I use floatfmt instead of intfmt then I get the result I think you're looking for.

    import pandas as pd
    test = {'2023m06': [2000], '2023m05': [100000]}
    df = pd.DataFrame.from_dict(test,
                                orient='index',
                                columns=['myColumn'])
    print(df.to_markdown(tablefmt='rounded_grid',floatfmt=',.0f'))
    
    ╭─────────┬────────────╮
    │         │   myColumn │
    ├─────────┼────────────┤
    │ 2023m06 │      2,000 │
    ├─────────┼────────────┤
    │ 2023m05 │    100,000 │
    ╰─────────┴────────────╯
    

    Including another example with multiple columns, since that was needed in my case.

    import pandas as pd
    test = {'2023m06': [2000], '2023m05': [100000]}
    df = pd.DataFrame.from_dict(test,
                                orient='index',
                                columns=['myColumn'])
    df['%'] = df['myColumn']/df['myColumn'].sum()
    print(df.to_markdown(tablefmt='rounded_grid',floatfmt=(None,',.0f','.2%')))
    
    ╭─────────┬────────────┬────────╮
    │         │   myColumn │      % │
    ├─────────┼────────────┼────────┤
    │ 2023m06 │      2,000 │  1.96% │
    ├─────────┼────────────┼────────┤
    │ 2023m05 │    100,000 │ 98.04% │
    ╰─────────┴────────────┴────────╯