Skip to content Skip to sidebar Skip to footer

Pandas Display: Truncate Column Display Rather Than Wrapping

With lengthy column names, DataFrames will display in a very messy form seemingly no matter what options are set. Info: I'm in Jupyter QtConsole, pandas 0.20.1, with the following

Solution 1:

Use max_columns

from string import ascii_letters

df = pd.DataFrame(np.random.randint(10, size=(5, 52)), columns=list(ascii_letters))

with pd.option_context(
    'display.max_colwidth', 20,
    'expand_frame_repr', False,
    'display.max_rows', 25,
    'display.max_columns', 5,
):
    print(df.add_prefix('really_long_column_name_'))

   really_long_column_name_a  really_long_column_name_b            ...              really_long_column_name_Y  really_long_column_name_Z
081                  ...                                19185                  ...                                21250                  ...                                99368                  ...                                09412                  ...                                71      

[5 rows x 52 columns]

Another idea... Obviously not exactly what you want, but maybe you can twist it to your needs.

d1 = df.add_suffix('_really_long_column_name')

with pd.option_context('display.max_colwidth', 4, 'expand_frame_repr', False):
    mw = pd.get_option('display.max_colwidth')
    print(d1.rename(columns=lambda x: x[:mw-3] + '...'iflen(x) > mw else x))

   a...  b...  c...  d...  e...  f...  g...  h...  i...  j...  ...   Q...  R...  S...  T...  U...  V...  W...  X...  Y...  Z...
06555835076   ...     906968406710547254387   ...     815359455327216510131   ...     670995282231871455883   ...     365710814047562497905   ...     6816354232

Solution 2:

Looks like it will need an enhancement. The relevant code in the repr function appears to be here:

    max_rows = get_option("display.max_rows")
    max_cols = get_option("display.max_columns")
    show_dimensions = get_option("display.show_dimensions")
    if get_option("display.expand_frame_repr"):
        width, _ = console.get_console_size()
    else:
        width = None
    self.to_string(buf=buf, max_rows=max_rows, max_cols=max_cols,
                   line_width=width, show_dimensions=show_dimensions)

So either you pass expand_frame_repr=True and it wraps on the line width, or you pass expand_frame_repr=False and it shouldn't. But it looks like there is a bug in the code (this should be pandas 0.20.3 iirc):

in pd.io.formats.format.DataFrameFormatter:

def_chk_truncate(self):
    """
    Checks whether the frame should be truncated. If so, slices
    the frame up.
    """from pandas.core.reshape.concat import concat

    # Column of which first element is used to determine width of a dot col
    self.tr_size_col = -1# Cut the data to the information actually printed
    max_cols = self.max_cols
    max_rows = self.max_rows

    if max_cols == 0or max_rows == 0:  # assume we are in the terminal# (why else = 0)
        (w, h) = get_terminal_size()
        self.w = w
        self.h = h
        if self.max_rows == 0:
            dot_row = 1
            prompt_row = 1if self.show_dimensions:
                show_dimension_rows = 3
            n_add_rows = (self.header + dot_row + show_dimension_rows +
                          prompt_row)
            # rows available to fill with actual data
            max_rows_adj = self.h - n_add_rows
            self.max_rows_adj = max_rows_adj

        # Format only rows and columns that could potentially fit the# screenif max_cols == 0andlen(self.frame.columns) > w:
            max_cols = w
        if max_rows == 0andlen(self.frame) > h:
            max_rows = h

Looks like it intended to do what you wanted, but was unfinished. It's checking max_cols against the number of columns, not the total width of the columns.

So you could either create a show_df function that would calculate the correct number of columns and show it in an option_context like pi2Squared's answer, or fix it here (and maybe submit a patch if you need it distributed).

Solution 3:

As others have pointed out, Pandas itself seems to be bugged or badly designed here, so a workaround is required.

Most of the time this problem occurs with numerical columns, since numbers are relatively short. Pandas will split the column heading onto multiple lines if there are spaces in it, so you can "hack in" the correct behavior by inserting spaces into column headings for numerical columns when you display the dataframe. I have a one-liner to do this:

defcolfix(df, L=5): return df.rename(columns=lambda x: ' '.join(x.replace('_', ' ')[i:i+L] for i inrange(0,len(x),L)) if df[x].dtype in ['float64','int64'] else x )

do display your dataframe, simply type

colfix(your_df)

note that the renaming is not going to permanently change the dataframe, it will only add spaces to the names for the purposes of displaying it that one time.

Results (in a Jupyter Notebook):

With colfix:

using colfix

Without:

without colfix

Post a Comment for "Pandas Display: Truncate Column Display Rather Than Wrapping"