Python 3.x 如果符合条件,如何使用python更改.xlsx单元格的颜色,如果不保留旧颜色

Python 3.x 如果符合条件,如何使用python更改.xlsx单元格的颜色,如果不保留旧颜色,python-3.x,pandas,python-2.7,xlsx,pandas-styles,Python 3.x,Pandas,Python 2.7,Xlsx,Pandas Styles,我首先使用pandas.style.background\u gradient对每行中的所有值进行渐变,但有些值是nan,函数将这些值的backgroundcolor设置为黑色。我试着用这样的方法来改变值 def black_to_white(val): if is_nan(val) or isinstance(val, str): color = 'white' else: color = same color return 'background-color: %s' %

我首先使用pandas.style.background\u gradient对每行中的所有值进行渐变,但有些值是nan,函数将这些值的backgroundcolor设置为黑色。我试着用这样的方法来改变值

def black_to_white(val):
if is_nan(val) or isinstance(val, str): 
    color = 'white'
else: 
    color = same color
return 'background-color: %s' % color 
但是在其他的舞台上,我不知道如何使用旧的颜色

这是我的密码

dfs =df.style.background_gradient(cmap='RdYlGn',axis=1)
dfs = df.style.applymap(black_to_white)

我想出了一个简单的解决办法。我刚才补充说

dfs =df.style.background_gradient(cmap='RdYlGn',axis=1).highlight_null('white')
它是有效的