Pretty printing newlines inside a string in a Pandas DataFrame
Using pandas .set_properties()
and CSS white-space
property
[For use in IPython notebooks]
Another way will be to use pandas's pandas.io.formats.style.Styler.set_properties() method and the CSS "white-space": "pre-wrap"
property:
from IPython.display import display
# Assuming the variable df contains the relevant DataFrame
display(df.style.set_properties(**{
'white-space': 'pre-wrap',
})
To keep the text left-aligned, you might want to add 'text-align': 'left'
as below:
from IPython.display import display
# Assuming the variable df contains the relevant DataFrame
display(df.style.set_properties(**{
'text-align': 'left',
'white-space': 'pre-wrap',
})
If you're trying to do this in ipython notebook, you can do:
from IPython.display import display, HTML
def pretty_print(df):
return display( HTML( df.to_html().replace("\\n","<br>") ) )