Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单元格着色后保留熊猫显示格式_Python_Pandas - Fatal编程技术网

Python 单元格着色后保留熊猫显示格式

Python 单元格着色后保留熊猫显示格式,python,pandas,Python,Pandas,在给单元格着色后,如何保留百分比格式 import pandas as pd sample_tab = pd.DataFrame({'a': [-0.1, 0.2], 'b': [0, 0.5]}) pd.options.display.float_format = '{:.1f}%'.format sample_tab.style.apply(lambda x: ["background: red" if v < 0 else

在给单元格着色后,如何保留百分比格式

import pandas as pd

sample_tab = pd.DataFrame({'a': [-0.1, 0.2],
                           'b': [0, 0.5]})

pd.options.display.float_format = '{:.1f}%'.format

sample_tab.style.apply(lambda x: ["background: red" if v < 0 else "background: green" if v > 0 else "" for v in x], axis = 1)
使用.format:.n%在应用apply函数后

sample_tab = pd.DataFrame({'a': [-0.132, 0.234], 'b': [0, 0.534]})
sample_tab.style.apply(lambda x: ["background: red" if v < 0 else "background: green" if v > 0 else "" for v in x], axis = 1).format("{:.2}%")
sample_tab.style.apply(lambda x: ["background: red" if v < 0 else "background: green" if v > 0 else "" for v in x], axis = 1).format("{:.3}%")