这是我的DataFrame:
import pandas as pd
df = pd.DataFrame(
{
'a': [2, 2, 2, -4, 4, 4, 4, -3, 2, -2, -6],
'b': [2, 2, 2, 4, 4, 4, 4, 3, 2, 2, 6]
}
)
当我使用to_excel
时,我使用一个函数来突出显示a
中的单元格:
def highlight_cells(s):
if s.name=='a':
conds = [s > 0, s < 0, s == 0]
labels = ['background-color: lime', 'background-color: pink', 'background-color: gold']
array = np.select(conds, labels, default='')
return array
else:
return ['']*s.shape[0]
现在,如果a
中的值为正,我想通过添加加号来添加另一个特征.例如,1变为+1.我希望此特性仅适用于第a
列.
这是我的try ,但它不起作用.它给我的错误是文章的标题.
df.style.apply(highlight_cells).style.format({'a': '{:+g}'}).to_excel('df.xlsx', sheet_name='xx', index=False)