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:
I am working in a Jupyter notebook, but would prefer a pandas-based solution, if possible.
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, .
You can rename the columns using the rename() method by using the axis keyword in it.
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.
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')])])
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.
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']]
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