Search code examples
pythonpandasxlsx

Pandas format symbols xlsx


I need a %symbol on my xlsx. I apply format2 = workbook.add_format({'num_format': '0%'}) but with that my numbers are multiplicated for 100 and I don´t want it! I need only add the %symbol

xlsx data:

1.72

format2:

172%

format I need:

1.72%

Solution

  • First divide your numerical columns by 100:

    df.loc[:, df.dtypes.apply(lambda c: np.issubdtype(c, np.number))] = \
         df.select_dtypes('number').div(100.)
    

    alternatively:

    cols = df.columns[df.dtypes.apply(lambda c: np.issubdtype(c, np.number))]
    df[cols] /= 100.
    

    now you can use your % format in Excel