问题
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.
回答1:
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')])])
回答2:
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.
回答3:
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']]
回答4:
You can "hack in" the correct behavior by inserting spaces into column headings with:
def colfix(df, L=5): return df.rename(columns=lambda x: ' '.join(x.replace('_', ' ')[i:i+L] for i in range(0,len(x),L)) if df[x].dtype in ['float64','int64'] else x )
colfix(your_df)
See my answer to a similar question https://stackoverflow.com/a/45078833/6903458
来源:https://stackoverflow.com/questions/43092345/wrapping-column-names-in-python-pandas-dataframe-or-jupyter-notebooks