pythonpandasgreat-tables

How to handle inf and nans in Great Table


I've got a dataframe that I want to format which includes inf and nan.

The dict for it is:

df = pd.DataFrame({'Foodbank': {0: 'study',
  1: 'generation',
  2: 'near',
  3: 'sell',
  4: 'former',
  5: 'line',
  6: 'ok',
  7: 'field',
  8: 'last',
  9: 'really',
  10: 'particularly',
  11: 'must',
  12: 'drive',
  13: 'herself',
  14: 'learn'},
 '%(LY)': {0: -20.93,
  1: -19.23,
  2: -26.09,
  3: 150.0,
  4: 90.24,
  5: -23.85,
  6: nan,
  7: inf,
  8: inf,
  9: inf,
  10: inf,
  11: -35.48,
  12: nan,
  13: nan,
  14: -1.3}})

from great_tables import GT
GT(df)

It looks like this:

enter image description here

What I want is to have a dash or n/a to highlight it rather than inf which won't mean anything to an audience.


Solution

  • A possible solution:

    df['%(LY)'] = df['%(LY)'].replace(np.inf, np.nan)
    

    Output:

            Foodbank   %(LY)
    0          study  -20.93
    1     generation  -19.23
    2           near  -26.09
    3           sell  150.00
    4         former   90.24
    5           line  -23.85
    6             ok     NaN
    7          field     NaN
    8           last     NaN
    9         really     NaN
    10  particularly     NaN
    11          must  -35.48
    12         drive     NaN
    13       herself     NaN
    14         learn   -1.30
    

    enter image description here