2017-12-08 2 views
0

세포 0 ~ 0.5조건부 서식 팬더 나는 녹색으로 1 이하로 세포에서 색을 어떻게

enter image description here

import pandas as pd 
import numpy as np 


df = pd.read_excel("C:\\13\\13\\13.xlsx") 
def highlight_vals(val, min=-1111, max=1, color='green'): 
    if min < val < max: 
     return 'background-color: %s' % color 
    else: 
     return '' 
df.style.applymap(highlight_vals, subset=['C']) 

def highlight_vals(val, min=0, max=0.5, color='yellow'): 
    if min < val < max: 
     return 'background-color: %s' % color 
    else: 
     return '' 
df.style.applymap(highlight_vals, subset=['C']) 


writer = pd.ExcelWriter("C:\\13\\13\\zz.xlsx") 
df.to_excel(writer, startrow=0, startcol=0, index = False) 

writer.save() 

감사합니다. 위의 코드는 색상 변경없이 Excel에 씁니다.

답변

1

이 작동합니다 :

import pandas as pd 
import numpy as np 


df = pd.read_excel("C:\\13\\13\\13.xlsx") 
def highlight_vals(val, min=-1111, max=1, color='green'): 
    if min <= val <= max: 
     return 'background-color: %s' % color 
    else: 
     return '' 
style_1 = df.style.applymap(highlight_vals, subset=['C']) 

def highlight_vals(val, min=0, max=0.5, color='yellow'): 
    if min <= val <= max: 
     return 'background-color: %s' % color 
    else: 
     return '' 
style_2 = df.style.applymap(highlight_vals, subset=['C']) 

style_1.use(style_2.export()) 

writer = pd.ExcelWriter("C:\\13\\13\\zz.xlsx") 
style_1.to_excel(writer, startrow=0, startcol=0, index = False) 

writer.save() 

나는 두 Styler 객체를 유지 다른 하나를 적용하고 dataframe 대신 Excel로 결과를 저장. 또한 귀하의 조건을 수정했음을 유의하십시오. 그렇지 않은 경우 노란색으로 표시되지 않습니다. min < val

+0

My Excel은 이것을 사용하여 변경되지 않습니다. –

+0

정말 확실합니까? 왜냐하면 나에게이 작품은 .. – Georgy

+0

긍정적입니다. 나는 창문을 사용하고 있습니다. 10. 그 코드를 정확히 복사했습니다. 어쩌면 그것의 잘못된 접근. 출력이 나타나지만 색상이 변하지 않습니다. –