Skip to content

Commit 329e59e

Browse files
committed
ENH: add justify option for column header, per #571
1 parent e4f3fa8 commit 329e59e

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pandas/core/format.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
sparsify : bool, optional
3131
Set to False for a DataFrame with a hierarchical index to print every
3232
multiindex key at each row, default True
33+
justify : {'left', 'right'}, default 'left'
34+
Left or right-justify the column labels
3335
index_names : bool, optional
3436
Prints the names of the indexes, default True """
3537

@@ -46,7 +48,8 @@ class DataFrameFormatter(object):
4648

4749
def __init__(self, frame, buf=None, columns=None, col_space=None,
4850
header=True, index=True, na_rep='NaN', formatters=None,
49-
float_format=None, sparsify=True, index_names=True):
51+
justify='left', float_format=None, sparsify=True,
52+
index_names=True):
5053
self.frame = frame
5154
self.buf = buf if buf is not None else StringIO()
5255
self.show_index_names = index_names
@@ -57,6 +60,7 @@ def __init__(self, frame, buf=None, columns=None, col_space=None,
5760
self.col_space = col_space
5861
self.header = header
5962
self.index = index
63+
self.justify = justify
6064

6165
if columns is not None:
6266
self.columns = _ensure_index(columns)
@@ -81,11 +85,21 @@ def to_string(self):
8185
str_index = self._get_formatted_index()
8286
str_columns = self._get_formatted_column_labels()
8387

84-
if self.header:
85-
stringified = [str_columns[i] + self._format_col(c)
86-
for i, c in enumerate(self.columns)]
87-
else:
88-
stringified = [self._format_col(c) for c in self.columns]
88+
stringified = []
89+
90+
for i, c in enumerate(self.columns):
91+
if self.header:
92+
fmt_values = self._format_col(c)
93+
cheader = str_columns[i]
94+
max_len = max(max(len(x) for x in fmt_values),
95+
max(len(x) for x in cheader))
96+
if self.justify == 'left':
97+
cheader = [x.ljust(max_len) for x in cheader]
98+
else:
99+
cheader = [x.rjust(max_len) for x in cheader]
100+
stringified.append(cheader + fmt_values)
101+
else:
102+
stringified = [self._format_col(c) for c in self.columns]
89103

90104
if self.index:
91105
to_write.append(adjoin(1, str_index, *stringified))

pandas/core/frame.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ def to_csv(self, path, sep=",", na_rep='', cols=None, header=True,
922922
def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
923923
header=True, index=True, na_rep='NaN', formatters=None,
924924
float_format=None, sparsify=True, nanRep=None,
925-
index_names=True):
925+
index_names=True, justify='left'):
926926
"""
927927
Render a DataFrame to a console-friendly tabular output.
928928
"""
@@ -944,6 +944,7 @@ def to_string(self, buf=None, columns=None, col_space=None, colSpace=None,
944944
formatters=formatters,
945945
float_format=float_format,
946946
sparsify=sparsify,
947+
justify=justify,
947948
index_names=index_names,
948949
header=header, index=index)
949950
formatter.to_string()

0 commit comments

Comments
 (0)