Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I left justify text in a pandas DataFrame column in an IPython notebook

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?

like image 592
Fred Mitchell Avatar asked Sep 11 '14 00:09

Fred Mitchell


People also ask

How do I left align a DataFrame column in Python?

In order to align columns to left in pandas dataframe, we use the dataframe. style. set_properties() function.

How do I change the text of a column in a DataFrame?

You can replace a string in the pandas DataFrame column by using replace(), str. replace() with lambda functions.

How do I trim text in pandas?

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, .


3 Answers

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.

like image 187
Brian Burns Avatar answered Oct 12 '22 16:10

Brian Burns


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
like image 32
unutbu Avatar answered Oct 12 '22 17:10

unutbu


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'))
like image 33
jwalton Avatar answered Oct 12 '22 15:10

jwalton