地形码如下:
import numpy as np
import pandas as pd
m=np.random.randint(1,20,size=(5,3 ))
df1=pd.DataFrame(m,columns=["a","b","c"])
##### function 1: color customization in data frame
def color_negative_red1(val):
if val > 9:
color = 'red'
else :
color = 'green'
return 'color: %s' % color
df1.style.applymap(color_negative_red1) #FİRST: show this in data frame
########### function 2: highlighted max values
def highlight_max(data, color='yellow'):
'''
highlight the maximum in a Series or DataFrame
'''
attr = 'background-color: {}'.format(color)
if data.ndim == 1: # Series from .apply(axis=0) or axis=1
is_max = data == data.max()
return [attr if v else '' for v in is_max]
else: # from .apply(axis=None)
is_max = data == data.max().max()
return pd.DataFrame(np.where(is_max, attr, ''),
index=data.index, columns=data.columns)
df1.style.apply(highlight_max, color='darkorange', axis=None) #SECOND: show this in same data frame too.
你好,朋友们,我想同时使用"color_negative_red1(val)“和突出显示同一数据帧的最大值函数。但就目前而言,我只能用其中一个作为数据框架。如何对相同的数据帧使用函数2的属性?
发布于 2021-03-28 07:14:48
你的意思是:
(df1.style.applymap(color_negative_red1) # FIRST
.apply(highlight_max, color='darkorange', axis=None) # SECOND
)
输出:
https://stackoverflow.com/questions/66842888
复制相似问题