Python Forum
pandas, tabulate, and alignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas, tabulate, and alignment
#1
I been playing around with pandas and tabulate but, I am not able to figure out how to left align the text other than the first column.
I've searched and found workarounds/hacks using the styling configure and none seemed to work for me.
Using code from a previous post I participated in:

import pandas as pd
from tabulate import tabulate

class Portfolio:
    def __init__(self):
        self.holdings = {}

    def buy(self, ticker, shares):
        self.holdings[ticker] = self.holdings.get(ticker, 0) + shares

    def sell(self, ticker, shares):
        self.holdings[ticker] = self.holdings.get(ticker, 0) - shares

    def __iter__(self):
        return iter(self.holdings.items())


p = Portfolio()
p.buy('Alpha', 15)
p.buy('Beta', 23)
p.buy('Gamma', 9)
p.buy('Gamma', 20)
p.sell('Beta', 5)


df = pd.DataFrame(p)
df.columns=['Stock', 'Shares']
print(tabulate(df, showindex=False, headers=df.columns))
# print('\n\n')
# print(df.to_string(index=False))


# str_len = len(max(p)[0])
#
# for (ticker, shares) in p:
#     if len(ticker) < str_len:
#         spacer = ' '*(str_len - len(ticker) + 2)
#     else:
#         spacer = ' '*2
#     print(f'{ticker} {spacer} {shares}')
As you can see the first column is aligned to the left. How do I get the rest of the columns to align left, either with tabulate or pandas?
Thanks for any help.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
print(tabulate(df, showindex=False, headers=df.columns, numalign="left"))
Output:
Stock Shares ------- -------- Alpha 15 Beta 18 Gamma 29
Or use one of the fished format setup.
print(tabulate(df, showindex=False, headers=df.columns, tablefmt="pretty"))
Output:
+-------+--------+ | Stock | Shares | +-------+--------+ | Alpha | 15 | | Beta | 18 | | Gamma | 29 | +-------+--------+
Reply
#3
Tabulate has a description of column alignment on the module page. You should be able to either set a default for all numeric columns (or string columns), or you can set it for each column individually.

Try:
print(tabulate(df, showindex=False, headers=df.columns, numalign="left"))
Reply
#4
Thanks guys. Guess I was making it harder that what it was.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code for alignment and font size 1418 0 326 Jan-14-2024, 03:56 AM
Last Post: 1418
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 511 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  make all text in Right to left alignment in tkinter widgets jalal0034 1 1,339 Sep-27-2022, 06:42 PM
Last Post: Larz60+
  Right to left alignment in python report using Reportlab jalal0034 1 1,847 Sep-27-2022, 04:25 AM
Last Post: jalal0034
  Space between list and column alignment rturus 8 5,132 Mar-17-2021, 04:47 PM
Last Post: rturus
  display the result of Dataframe in tabulate format alex80 0 1,396 Sep-09-2020, 02:22 PM
Last Post: alex80
  How to tabulate correctly repeated blocks? Xiesxes 4 2,961 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  Trying to Tabulate Information from an Aircraft Website Link(s) eddywinch82 35 13,676 Jun-25-2019, 09:40 PM
Last Post: snippsat
  Data alignment in Python Nirmal 1 2,641 Feb-12-2019, 09:55 PM
Last Post: nilamo
  OpenPyxl Cell.value Alignment pcsailor 0 8,833 Sep-10-2018, 01:09 AM
Last Post: pcsailor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020