to_markdown doesn't pass intfmt to tabulate

315 Views Asked by At

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?

2

There are 2 best solutions below

0
aVeryNiceCactus On BEST ANSWER

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% │
╰─────────┴────────────┴────────╯
1
Saad Ahmed On

When using df.to_markdown() in Pandas, the intfmt parameter does not support comma separation by default. It is designed to format integer values as plain numbers. To achieve comma separation in the markdown output, you can preprocess the DataFrame column before calling to_markdown(). Here's an example:

import pandas as pd

test = {'2023m06': [2000], '2023m05': [100000]}
df = pd.DataFrame.from_dict(test, orient='index', columns=['myColumn'])

# Preprocess the column to include comma separation
df['myColumn'] = df['myColumn'].apply(lambda x: format(x, ","))

# Convert DataFrame to markdown with comma-separated integers
markdown_output = df.to_markdown(tablefmt='rounded_grid')

print(markdown_output)

The above code uses a lambda function with format(x, ",") to convert the integers in the 'myColumn' column to strings with comma separation. Then, df.to_markdown() is called on the modified DataFrame, resulting in the desired markdown output with commas for the integer values.

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