Python Pandas 样式突出显示具有不同条件的每列的特定单元格
Python Pandas style highlight specific cells for each column with different condition
我正在尝试突出显示具有不同条件的每一列的特定单元格,它们的值与每一行的条件相匹配。
下图是我想要实现的:
The table I attempt to achieve
我搜索了 google 和 Whosebug,但其中 none 可以满足我的要求。任何熟悉 Pandas 风格的人都可以提供帮助吗?
以下是我尝试过但失败的代码:
Ex1
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
def highlight(s):
return ['background-color: yellow' if (v>2) else 'background-color: white' for v in s]
df.style.apply(highlight, axis=0)
Ex2
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
Column_limit = (df['A'] > 6) | (df['B'] > 2) | (df['C'] < 3)
df[Column_limit].style.applymap(lambda x: 'background-color: yellow', subset=pd.IndexSlice[:, ['A', 'C']])
Ex3
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
subsets = pd.IndexSlice[:, 'A']
df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)
如果有相同数量的条件,例如某些列数,请使用:
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
def highlight(x):
c1 = 'background-color: yellow'
# condition
m = pd.concat([(x['A'] > 6), (x['B'] > 2), (x['C'] < 3)], axis=1)
#print (m)
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
return df1.mask(m, c1)
df.style.apply(highlight, axis=None)
如果有很多列并且只需要处理其中的一部分:
def highlight(x):
c1 = 'background-color: yellow'
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
df1.loc[(x['A'] > 6), 'A'] = c1
df1.loc[(x['B'] > 2), 'B'] = c1
df1.loc[(x['C'] < 3), 'C'] = c1
return df1
df.style.apply(highlight, axis=None)
编辑:
如果需要指定所有掩码但在最后一步过滤器中只有一些列使用:
def highlight(x):
c1 = 'background-color: yellow'
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
df1.loc[(x['A'] > 6), 'A'] = c1
df1.loc[(x['B'] > 2), 'B'] = c1
df1.loc[(x['C'] < 3), 'C'] = c1
need = ['A','C']
df1 = df1[need].reindex(x.columns, fill_value='', axis=1)
return df1
或者删除不需要的掩码:
def highlight(x):
c1 = 'background-color: yellow'
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
df1.loc[(x['A'] > 6), 'A'] = c1
df1.loc[(x['C'] < 3), 'C'] = c1
return df1
df.style.apply(highlight, axis=None)
我正在尝试突出显示具有不同条件的每一列的特定单元格,它们的值与每一行的条件相匹配。
下图是我想要实现的: The table I attempt to achieve
我搜索了 google 和 Whosebug,但其中 none 可以满足我的要求。任何熟悉 Pandas 风格的人都可以提供帮助吗?
以下是我尝试过但失败的代码:
Ex1
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
def highlight(s):
return ['background-color: yellow' if (v>2) else 'background-color: white' for v in s]
df.style.apply(highlight, axis=0)
Ex2
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
Column_limit = (df['A'] > 6) | (df['B'] > 2) | (df['C'] < 3)
df[Column_limit].style.applymap(lambda x: 'background-color: yellow', subset=pd.IndexSlice[:, ['A', 'C']])
Ex3
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
subsets = pd.IndexSlice[:, 'A']
df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)
如果有相同数量的条件,例如某些列数,请使用:
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
def highlight(x):
c1 = 'background-color: yellow'
# condition
m = pd.concat([(x['A'] > 6), (x['B'] > 2), (x['C'] < 3)], axis=1)
#print (m)
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
return df1.mask(m, c1)
df.style.apply(highlight, axis=None)
如果有很多列并且只需要处理其中的一部分:
def highlight(x):
c1 = 'background-color: yellow'
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
df1.loc[(x['A'] > 6), 'A'] = c1
df1.loc[(x['B'] > 2), 'B'] = c1
df1.loc[(x['C'] < 3), 'C'] = c1
return df1
df.style.apply(highlight, axis=None)
编辑:
如果需要指定所有掩码但在最后一步过滤器中只有一些列使用:
def highlight(x):
c1 = 'background-color: yellow'
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
df1.loc[(x['A'] > 6), 'A'] = c1
df1.loc[(x['B'] > 2), 'B'] = c1
df1.loc[(x['C'] < 3), 'C'] = c1
need = ['A','C']
df1 = df1[need].reindex(x.columns, fill_value='', axis=1)
return df1
或者删除不需要的掩码:
def highlight(x):
c1 = 'background-color: yellow'
#empty DataFrame of styles
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#set new columns by condition
df1.loc[(x['A'] > 6), 'A'] = c1
df1.loc[(x['C'] < 3), 'C'] = c1
return df1
df.style.apply(highlight, axis=None)