How to format IPython html display of Pandas dataframe?

前端 未结 3 1349
囚心锁ツ
囚心锁ツ 2020-11-30 22:12

How can I format IPython html display of pandas dataframes so that

  1. numbers are right justified
  2. numbers have commas as thousands separator
  3. lar
3条回答
  •  爱一瞬间的悲伤
    2020-11-30 22:30

    This question was asked a long time ago. Back then, pandas didn't yet include pd.Styler. It was added in version 0.17.1.

    Here's how you would use this to achieve your desired goal and some more:

    • Center the header
    • right-align any number columns
    • left-align the other columns.
    • Add a formatter for the numeric columns like you want
    • make it so that each column has the same width.

    Here's some example data:

    In [1]:
    df = pd.DataFrame(np.random.rand(10,3)*2000, columns=['A','B','C'])
    df['D'] = np.random.randint(0,10000,size=10)
    df['TextCol'] = np.random.choice(['a','b','c'], 10)
    df.dtypes
    
    Out[1]:
    A          float64
    B          float64
    C          float64
    D            int64
    TextCol     object
    dtype: object
    

    Let's format this using df.style:

    # Construct a mask of which columns are numeric
    numeric_col_mask = df.dtypes.apply(lambda d: issubclass(np.dtype(d).type, np.number))
    
    # Dict used to center the table headers
    d = dict(selector="th",
        props=[('text-align', 'center')])
    
    # Style
    df.style.set_properties(subset=df.columns[numeric_col_mask], # right-align the numeric columns and set their width
                            **{'width':'10em', 'text-align':'right'})\
            .set_properties(subset=df.columns[~numeric_col_mask], # left-align the non-numeric columns and set their width
                            **{'width':'10em', 'text-align':'left'})\
            .format(lambda x: '{:,.0f}'.format(x) if x > 1e3 else '{:,.2f}'.format(x), # format the numeric values
                    subset=pd.IndexSlice[:,df.columns[numeric_col_mask]])\
            .set_table_styles([d]) # center the header
    


    Note that instead of calling .format on the subset columns, you can very well set the global default pd.options.display.float_format instead:

    pd.options.display.float_format = lambda x: '{:,.0f}'.format(x) if x > 1e3 else '{:,.2f}'.format(x)
    

提交回复
热议问题