import pandas as pd
import numpy as np
data = [['benzine', 1], ['benzine', 0],
['diesel', 1], ['diesel', 0], ['diesel', 1],
['electro', 1], ['electro', 0], ['electro', 1],['electro', 1]]
df = pd.DataFrame(data, columns = ['engine_type', 'rented'])
pt = df.pivot_table(index=['engine_type'], values= ['rented'], aggfunc=[np.mean])
pt.set_axis(['percent_on_rent'], axis = 'columns', inplace = True)
pt.style.format({'percent_on_rent': '{:.0%}'})
print(pt)
Current output:
percent_on_rent
engine_type
benzine 0.500000
diesel 0.666667
electro 0.750000
Expected output:
percent_on_rent
engine_type
benzine 50%
diesel 67%
electro 75$
NB: The following code
print(pt.to_string(float_format=lambda x: '{:.0%}'.format(x)))
works but I'd like to use .style.format(...
to format several columns using different formatting styles as well as to set output table columns' (wrapped) captions.
[UPDATE]
Added:
print(pd.versions())
Result:
INSTALLED VERSIONS
------------------
commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : AMD64 Family 23 Model 24 Stepping 1, AuthenticAMD
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.0.5
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.3.0
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None
None