8

I am using tabulate (https://pypi.org/project/tabulate/) and I want to format a number column with thousand separator and right align it. This is what I have done and it doesn't right align my column.

import pandas as pd
rom tabulate import tabulate

df = pd.DataFrame([{'size':225612466, 'path': '/etc'}, {'size':66, 'path': '/home'}])
df['size'] = df['size'].apply(lambda x: "{:,}".format(x).rjust(15))
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False))

+--------+-------------+
| path   | size        |
|--------+-------------|
| /etc   | 225,612,466 |
| /home  | 66          |
+--------+-------------+

I would like it to be like this:

+--------+-----------------+
| path   | size            |
|--------+-----------------|
| /etc   |     225,612,466 |
| /home  |              66 |
+--------+-----------------+

Thanks,

Sean Nguyen
  • 12,528
  • 22
  • 74
  • 113

3 Answers3

4

I guess the missing parameter in tabulate is numalign="right" but you have to get rid of manual rjust which changes the values to str:

import pandas as pd
rom tabulate import tabulate

df = pd.DataFrame([{'size':225612466, 'path': '/etc'}, {'size':66, 'path': '/home'}])
print(tabulate(df, headers='keys', tablefmt='psql', showindex=False, numalign="right"))

Gives output:

+--------+-----------+
| path   |      size |
|--------+-----------|
| /etc   | 225612466 |
| /home  |        66 |
+--------+-----------+

If you want to keep the thousands separators you have to keep the formatting and align strings to the rigth using stralign:

print(tabulate(df, headers='keys', tablefmt='psql', showindex=False, stralign="right"))
sophros
  • 14,672
  • 11
  • 46
  • 75
  • 1
    But this one doesn't do separator on thousand. Do you know how to do that? – Sean Nguyen Nov 15 '18 at 18:45
  • Can we set the alignment for just one column (size)? – Sean Nguyen Nov 15 '18 at 19:47
  • This really should be another question. And you should have demonstrated some effort of solving the issue. An obvious thing you can try is you can pad your paths on the right so that they appear left-aligned. – sophros Nov 15 '18 at 19:52
  • Sorry, i wasn't clear on my question. You can see on my desired result, the path is left align and i only want to right align my size column with thousand separator. This is just sample dataframe. My data frame has a lot more columns so having the ability to adjust configuration on one column is more desired. – Sean Nguyen Nov 15 '18 at 20:25
  • Then I guess your only option is going the way I suggested - manually aligning the individual columns. You can however pick which columns to align manually and the rest with the `tabulate`'s parameter (e.g. 10 string columns + 2 numeric -> you manually align the 2 numeric ones and use `stralign="left"`). – sophros Nov 15 '18 at 20:38
4

The numbers have all become strings. Add the option: colalign("right", "left"). Let's test the suggestion...

print(tabulate(df, headers='keys', tablefmt='psql', showindex=False, colalign=("right", "left")))
+-------------+--------+
|        size | path   |
|-------------+--------|
| 225,612,466 | /etc   |
|          66 | /home  |
+-------------+--------+

Bingo, bingo. It is in their docs.

sweetser
  • 57
  • 3
0

Usage of tabulate

tabulate(
    tabular_data, 
    headers, tablefmt, 
    floatfmt, 
    numalign,  # ---> here
    stralign, 
    missingval, 
    showindex, 
    disable_numparse, 
    colalign, 
    maxcolwidths
)

So that, to make number align left you can use numalign="left"

Binh Ho
  • 3,690
  • 1
  • 31
  • 31