pandas display: truncate column display rather than wrapping

后端 未结 3 1101
醉酒成梦
醉酒成梦 2021-01-19 03:43

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, wit

3条回答
  •  执笔经年
    2021-01-19 04:12

    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:

    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 )
    

    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

提交回复
热议问题