I am trying to format the output in an IPython notebook. I tried using the to_string function, and this neatly lets me eliminate the index column. But the textual data is right justified.
In [10]:
import pandas as pd
columns = ['Text', 'Value']
a = pd.DataFrame ({'Text': ['abcdef', 'x'], 'Value': [12.34, 4.2]})
print (a.to_string (index=False))
Text Value
abcdef 12.34
x 4.20
The same is true when just printing the dataframe.
In [12]:
print (a)
Text Value
0 abcdef 12.34
1 x 4.20
The justify argument in the to_string function, surprisingly, only justifies the column heading.
In [13]:
import pandas as pd
columns = ['Text', 'Value']
a = pd.DataFrame ({'Text': ['abcdef', 'x'], 'Value': [12.34, 4.2]})
print (a.to_string (justify='left', index=False))
Text Value
abcdef 12.34
x 4.20
How can I control the justification settings for individual columns?
In order to align columns to left in pandas dataframe, we use the dataframe. style. set_properties() function.
You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.
lstrip() is used to remove spaces from the left side of string, str. rstrip() to remove spaces from right side of the string and str. strip() removes spaces from both sides. Since these are pandas function with same name as Python's default functions, .
If you're willing to use another library, tabulate will do this -
$ pip install tabulate
and then
from tabulate import tabulate
df = pd.DataFrame ({'Text': ['abcdef', 'x'], 'Value': [12.34, 4.2]})
print(tabulate(df, showindex=False, headers=df.columns))
Text Value
------ -------
abcdef 12.34
x 4.2
It has various other output formats also.
You could use a['Text'].str.len().max()
to compute the length of the longest string in a['Text']
, and use that number, N
, in a left-justified formatter '{:<Ns}'.format
:
In [211]: print(a.to_string(formatters={'Text':'{{:<{}s}}'.format(a['Text'].str.len().max()).format}, index=False))
Text Value
abcdef 12.34
x 4.20
I like @unutbu's answer (not requiring any additional dependencies). @JS.'s additions are a step in the direction (towards something re-usable).
Since the construction of the formatter dict is the difficult part, let's create a function which creates the formatter dict from a DataFrame and an optional list of columns to format.
def make_lalign_formatter(df, cols=None):
"""
Construct formatter dict to left-align columns.
Parameters
----------
df : pandas.core.frame.DataFrame
The DataFrame to format
cols : None or iterable of strings, optional
The columns of df to left-align. The default, cols=None, will
left-align all the columns of dtype object
Returns
-------
dict
Formatter dictionary
"""
if cols is None:
cols = df.columns[df.dtypes == 'object']
return {col: f'{{:<{df[col].str.len().max()}s}}'.format for col in cols}
Let's create some example data to demonstrate using this function:
import pandas as pd
# Make some data
data = {'First': ['Tom', 'Dick', 'Harry'],
'Last': ['Thumb', 'Whittington', 'Potter'],
'Age': [183, 667, 23]}
# Make into a DataFrame
df = pd.DataFrame(data)
To align all the columns of type object in our DataFrame:
# Left align all columns
print(df.to_string(formatters=make_lalign_formatter(df),
index=False,
justify='left'))
To align only the 'First'
column:
# Left align 'First' column
print(df.to_string(formatters=make_lalign_formatter(df, cols=['First']),
index=False,
justify='left'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With