pythonexcelpandas

Pandas Styler.bar() not showing on Excel column


I'm working on a default style for some reports I have to do. I'd like to add the Styler.bar() method.

Sample of the dataset used for integration:

symbol,date,open,high,low,close,volume
AAL,2014-01-02,25.07,25.82,25.06,25.36,8998943
AAPL,2014-01-02,79.3828,79.5756,78.8601,79.0185,58791957
AAP,2014-01-02,110.36,111.88,109.29,109.74,542711
ABBV,2014-01-02,52.12,52.33,51.52,51.98,4569061
ABC,2014-01-02,70.11,70.23,69.48,69.89,1148391
ABT,2014-01-02,38.09,38.4,38,38.23,4967472
ACN,2014-01-02,81.5,81.92,81.09,81.13,2405384
ADBE,2014-01-02,59.06,59.53,58.94,59.29,2746370
ADI,2014-01-02,49.52,49.75,49.04,49.28,2799092

If I test it in a Jupyter notebook works just fine but when I try to export it to Excel file, the columns do not change.

import pandas as pd

df = pd.read_csv('./sp500.csv')

styler = df.style

styler.bar(subset=['close'], color='#6399ae',
          vmin=0, vmax=df['close'].max())

There is no that much documentation about Styler nor this method and I fear that there is something superbasic that I'm missing on.

styler.to_excel('./sp500.csv', index=False)

Solution

  • The reason your Styler.bar() output isn’t showing up in Excel is because Excel doesn’t support the CSS-based styles that Styler uses in Jupyter. Only a limited subset of Styler features can be exported with .to_excel(), and bar() isn’t one of them.
    Source of truth: [https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Export-to-Excel]

    If you want in-cell bars inside Excel, you need to use Excel’s own conditional formatting (data bars).