Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping column names in Python Pandas DataFrame or Jupyter Notebooks

I have long titles for some of my columns in my data frame, and I would like the ability to wrap the text. I know that this functionality is built into pandas, as I do:

pd.DataFrame(np.random.randn(2, 10), 
    columns=['Very Long Column Title ' + str(i) for i in range(10)])

DataFrame with wrapped column names

But if I have fewer columns, the titles will not wrap:

pd.DataFrame(np.random.randn(10, 2), 
    columns=['Very Long Column Title ' + str(i) for i in range(2)])

DataFrame does not wrap column names

I have also tried to manually insert a newline:

import pandas as pd    
pd.DataFrame(np.random.randn(10, 2), 
    columns=['Very Long \n Column Title ' + str(i) for i in range(2)])

But that gives the same output as above.

I've found similar for answers on this topic:

  • Can I set variable column widths in pandas?
    will truncate column widths, but will not affect the title and will not wrap the text
  • Pretty printing newlines inside a string in a Pandas DataFrame
    This again touches on column contents but not the title

I am working in a Jupyter notebook, but would prefer a pandas-based solution, if possible.

like image 655
Rachel Avatar asked Mar 29 '17 11:03

Rachel


People also ask

How do you wrap text in a DataFrame in Python?

Pandas str. wrap() is an important method when dealing with long text data (Paragraphs or messages). This is used to distribute long text data into new lines or handle tab spaces when it exceeds the passed width. Since this is a string method, .

How do you name columns in a Jupyter notebook?

You can rename the columns using the rename() method by using the axis keyword in it.

How do you get the names of the columns of a DataFrame in Python?

To access the names of a Pandas dataframe, we can the method columns(). For example, if our dataframe is called df we just type print(df. columns) to get all the columns of the Pandas dataframe.


3 Answers

Here is an answer that does not involve changing the IPython properties:

df = pd.DataFrame(np.random.randn(10, 2), 
    columns=['Very Long Column Title ' + str(i) for i in range(2)])
df.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
like image 145
AndreyF Avatar answered Oct 17 '22 17:10

AndreyF


Jupyter notebooks inherit their display properties from a number of sources. There is no property in pandas that restricts the width of the column headers because pandas is not what causes the text to wrap, it is actually the rendered HTML.

You can overwrite the default Jupyter Notebook styles to restrict the maximum width of the table headers using:

from IPython.core.display import HTML
HTML("<style>.rendered_html th {max-width: 120px;}</style>")

Run this code once at the top of your notebook to set the max column width of html table headers to 120 pixels.

like image 6
James Avatar answered Oct 17 '22 17:10

James


Alternatively, you could use the package textwrap:

import textwrap

cols = ['Very Long Column Title ' + str(i) for i in range(2)]

# Split wide columns, you can then join these with any delimiter you'd like
cols = [textwrap.wrap(x, width=20) for x in cols]

# print(cols)
# [['Very Long Column', 'Title 0'], ['Very Long Column', 'Title 1']]

like image 1
Yaakov Bressler Avatar answered Oct 17 '22 16:10

Yaakov Bressler