Search code examples
pythonpandasdataframejupyter-notebookwidth

Pandas does not set column width (In Jupiter Notebook /Lab)


I want to show each row of pandas DataFrame in One line, and as I searched, it seems the solution is to set the display column width as None. However, it does not do the job for me.

Here is a test code to show the problem. I need to have each row in a single line (so basically I expect it to increase the columns width to a point where 'It issssssssssssssssss justttttttttttttttttttttt a tesssssssssssst' is shown in one line and not multiple lines).

import pandas as pd
pd.set_option('display.max_colwidth', None)
A=pd.DataFrame(['It issssssssssssssssss justttttttttttttttttttttt a tesssssssssssst', 'It is just a test'] )
df=pd.concat([A,A,A,A,A,A,A,A], axis=1)
df

enter image description here


Solution

  • One option (the only one?) is to change the CSS white-space to "nonwrap" with set_properties :

    (
        df
            .set_axis(range(len(df.columns)), axis=1)
            .style.set_properties(
                **{"white-space": "nowrap"}
            )
    )
    

    Output :

    enter image description here

    Update :

    With jupyter notebook, you can make a custom.css and put it in the default-config-dir :

    .dataframe td {
        white-space: nowrap;
    }
    

    The tree should look like this (in Windows) :

    C:
    ┗━━ Users
        ┗━━ Timeless
            ┗━━ .jupyter
                ┣━━ custom         # <-- make a new folder
                    ┗━━ custom.css # <-- put there the css
    

    Output :

    enter image description here