【问题标题】:styler has no attributestyler has no attribute
【发布时间】:2022-12-02 05:03:44
【问题描述】:

After formatting a dataframe, I'm failing at replacing the nan's with blanks. Where/how do I apply the fillna('') in this final step?

col_format = {'2021 Unique Members':'{:,.0f}', '2022 Unique Members':'{:,.0f}', '2021 Total Visits':'{:,.0f}',
            '2022 Total Visits':'{:,.0f}', '2022 Annualized Unique Members':'{:,.0f}', '2022 Annualized Total Visits':'{:,.0f}',
            'Anticipated Member % Increase*':'{:,.2%}', 'Anticipated Visit % Increase*':'{:,.2%}', 'Visits per Unique Member**':'{:,.2f}'}

       
df_wide_styled = df_wide.style.\
    format(col_format).\
    hide(axis='index').\
    set_caption('Behavioral Health Vendor Utilization').\
    set_table_styles(   
    [{'selector': 'th',
      'props': [('background', '#78BE20'), ('color', 'black'), ('font-weight', 'bold'), ('text-align', 'center'), ('max-width', '140px')]},
     {'selector': 'th.row_heading',
     'props': [('background', 'white'), ('color', 'black'),('font-weight', 'bold') ]},
     {'selector': 'caption',
    'props': [('color', 'Black'), ('font-size', '18px'), ('text-align','center'), ('border', '1px black solid !important')]}
    ]
    ).\
    set_properties(**{'text-align': 'center'}).\

df_wide_styled = df_wife_styled.fillna('') throws the 'Styer' has no attribute 'fillna' error

【问题讨论】:

  • can you do the filling before styling df_wide_styled = df_wide.fillna('').style.\ ... ?
  • nope, says Unknown format code 'f' for object of type 'str
  • Thanks @Stef! see below

标签: pandas dataframe styler


【解决方案1】:

turns out putting this line above the code used to create my pivot table above did the trick.

df_wide.replace(np.inf, 0, inplace=True)

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2022-12-01
    • 2022-12-01
    • 2020-04-26
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2022-12-27
    相关资源
    最近更新 更多