0

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?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
aeroNotAuto
  • 260
  • 3
  • 13
  • It seems your actual question is why `to_markdown` doesn't format thousands with commas. Integers have no commas. Commas are added when the numbers get formatted into strings for display – Panagiotis Kanavos Jul 14 '23 at 19:44
  • @PanagiotisKanavos I'm unsure what you mean; could you clarify? Without the DataFrame, `tabulate` is able to format the integers as strings with commas. With the DataFrame, `tabulate` appears to ignore the instruction to format the integers as strings with commas. – aeroNotAuto Jul 14 '23 at 19:47
  • Integers have no commas, they're binary values. `to_markdown` *formats* the integers into strings, using the parameters you passed. It does that using `tabluate`. If you check the [to_markdown](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_markdown.html) docs you'll see there are no `tablefmt` or `intfmt` parameters. These are passed (or should be passed) directly to `tabulate` through kwargs – Panagiotis Kanavos Jul 14 '23 at 19:50

2 Answers2

1

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% │
╰─────────┴────────────┴────────╯
0

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    │
╰─────────┴────────────╯

Saad Ahmed
  • 74
  • 1
  • 6
  • I appreciate the workaround, but is `intfmt` not supposed to be passed to `tabulate` and work the way it does when used directly with `tabulate`? Is this in the docs somewhere? I may have missed it. – aeroNotAuto Jul 14 '23 at 22:27