Feb-04-2022, 08:53 PM
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:
Thanks for any help.
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
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
