df = pd.DataFrame(...)
def highlight(row):
if row[0] > row[1]:
color = 'red'
elif row[0] < row[1]:
color = 'green'
else:
color = 'yellow'
background = ['background-color: {}'.format(color) for row in df['col1']]
return background
df2 = df.style.apply(highlight, axis = 1)
df2.to_excel('style.xlsx')
그건 그렇고, 나는 col1과 col2를 비교하고 싶습니다.
당신의 도움을 기다리고 있습니다.
열의 배경을 반환하는 대신 행을 반환합니다.
np.random.seed(1)
df = pd.DataFrame(np.random.randint(0,3, (10,4)),
columns=('hour','col1','col2','col3'))
def highlight(row):
if row['col1'] > row['col2']:
color='red'
elif row['col1'] < row['col2']:
color='green'
else: color = 'yellow'
# notice the difference
# also color:black is not needed, just add because my jupyter style
background=['',f'background-color:{color}; color:black','','']
return background
df.style.apply(highlight,axis=1)
산출:
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다