In this short post, you'll see how to pretty print newlines inside the values in Pandas DataFrame. Brief example is included for demonstration purposes.

You can find the two ways below:

(1) Using df.to_html().replace

from IPython.display import display, HTML

display( HTML( df.to_html().replace("\\n","<br>")))

(2) By

    'text-align': 'left',
    'white-space': 'pre-wrap',

Suppose we have a DataFrame like:

name url url2

Let's create new column which is concatenation of all others with separator a new line:

df['all'] = df['name'] + '\n' + df['url'] + '\n' + df['url2']


url2 all Softhints\n\n DataScientyst\n\n

As you can see default print is showing the string values as a single line - ignoring the newlines.


If you like to print newlines you need to use the following approach:

To print or display new lines inside columns in Pandas DataFrame you can: * convert the output to HTML

  • replace all new lines with <br> - HTML tag for new line
  • display the result as HTML
from IPython.display import display, HTML

display( HTML( df.to_html().replace("\\n","<br>") ) )

now all newlines symbols are printed or displayed in Jupyter cells correctly:

url2 all
0 Softhints
1 DataScientyst

You can define also a method like:

from IPython.display import display, HTML

def print_newlines(df):
    return display( HTML( df.to_html().replace("\\n","\<br\>") ) )


Alternative method is by changing Pandas properties:

  • white-space
  • text-align

Let say you would like to have newlines and centered values. Then you can use style.set_properties with the following values:**{
    'text-align': 'center',
    'white-space': 'pre-wrap',