我有以下 Dataframe :
dic = {'US':{'Traffic':{'new':1415, 'repeat':670}, 'Sales':{'new':67068, 'repeat':105677}},
'UK': {'Traffic':{'new':230, 'repeat':156}, 'Sales':{'new':4568, 'repeat':10738}}}
d1 = defaultdict(dict)
for k, v in dic.items():
for k1, v1 in v.items():
for k2, v2 in v1.items():
d1[(k, k2)].update({k1: v2})
df.insert(loc=2, column=' ', value=None)
df.insert(loc=0, column='Mode', value='Website')
df.columns = df.columns.rename("Metric", level=1)
它看起来像:
我需要使用以下函数中的条件将字体和背景色应用于数据框的流量和销售行的帮助:
def sales_color(val):
font_color = ''
background_color = ''
if val <= 10000:
font_color = 'red'
background_color = 'light red'
elif val >= 100000:
font_color = 'green'
else:
font_color = 'grey'
return [font_color, background_color]
def traffic_color(val):
font_color = 'orange' if val < 300 else 'black'
background_color = 'light orange' if val < 300 else ''
return [font_color, background_color]
我尝试了一种效率低下的方法-将颜色单独应用于单元格,但这不起作用:
df['US']['new']['Sales'].style.apply(sales_color)
df['US']['new']['Traffic'].style.apply(traffic_color)
df['US']['Repeat']['Sales'].style.apply(sales_color)
df['US']['Repeat']['Traffic'].style.apply(traffic_color)
df['UK']['new']['Sales'].style.apply(sales_color)
df['UK']['new']['Traffic'].style.apply(traffic_color)
df['UK']['Repeat']['Sales'].style.apply(sales_color)
df['UK']['Repeat']['Traffic'].style.apply(traffic_color)
1条答案
按热度按时间2izufjch1#
使用自定义函数,通过
DataFrame.loc
选择,然后通过numpy.where
和numpy.select
按条件设置值。对于不使用
light red
和light orange
颜色的我,我使用colors hex codes代替: