Python 如果另一个单元格超出其值,则为整列设置颜色格式单元格
Python to color format cell if another cell exceeds it's value, for an entire column
我有一个数据框 (df),在“评级”列中有一个值,在“600”列中有另一个值。我想遍历整个 df 并根据其大于还是小于列“600”中的值对称为“评级”的整个列进行颜色编码。如果我将评级列与设定值进行比较,我已经能够做到这一点,但没有运气遍历列标题“600”中的每个唯一值。
for index, row in df.iterrows():
if row[600] == float:
df.styled=df.style\
.applymap(lambda x: 'background-color: %s' % 'crimson' if row['600'] > row['Rating']
else 'background-color: %s' % 'orange' if row['600'] < row['Rating'])
我也尝试过这种方法,但没有成功:
def HIGHLIGHT(row):
red = 'background-color: red;'
blue = 'background-color: blue;'
green = 'background-color: green;'
if row['600'] > row['Rating']:
return [red, blue]
elif row['600'] < row['Rating']:
return [blue, red]
else:
return [green, green]
df.style.apply(HIGHLIGHT, subset=['600', 'Rating'], axis=1)
当我这样做时:
ratings = [9,8,3,5,6]
the600 = [10, 6, 5, 2, 1]
df = pd.DataFrame([ratings, the600]).T
df.columns = ['Rating', '600']
def HIGHLIGHT(row):
red = 'background-color: red;'
blue = 'background-color: blue;'
green = 'background-color: green;'
if row['600'] > row['Rating']:
return [red, blue]
elif row['600'] < row['Rating']:
return [blue, red]
else:
return [green, green]
df.style.apply(HIGHLIGHT, subset=['600', 'Rating'], axis=1)
我明白了:
如果它不适合您,我建议您使用 df.dtypes
检查数据类型。例如,如果我按以下方式更改其中一个评级值:
ratings = [9,8,3,"5",6]
我收到这个错误:
TypeError: '>' not supported between instances of 'str' and 'int'
@le-camerone,你的回答帮助我弄清楚发生了什么。我将整个 table 作为浮点值,由于某些原因,这种类型的条件格式在浮点数之间不起作用,但是,在转换为整数后,我能够得到预期的结果。这是我在将浮点数转换为整数的代码之前包含的内容。
df['600'] =pd.to_numeric(df['600'], errors = 'coerce')
df['Rating'] =pd.to_numeric(df['Rating'], errors = 'coerce')
df = df.dropna(subset=['600'])
df = df.dropna(subset=['Rating'])
df['600'] = df['600'].astype(int)
df['Rating'] = df['Rating'].astype(int)
def HIGHLIGHT(row):
red = 'background-color: red;'
blue = 'background-color: blue;'
green = 'background-color: green;'
if row['600'] > row['Rating']:
return [red, blue]
elif row['600'] < row['Rating']:
return [blue, red]
else:
return [green, green]
df.style.apply(HIGHLIGHT, subset=['600', 'Rating'], axis=1)
我有一个数据框 (df),在“评级”列中有一个值,在“600”列中有另一个值。我想遍历整个 df 并根据其大于还是小于列“600”中的值对称为“评级”的整个列进行颜色编码。如果我将评级列与设定值进行比较,我已经能够做到这一点,但没有运气遍历列标题“600”中的每个唯一值。
for index, row in df.iterrows():
if row[600] == float:
df.styled=df.style\
.applymap(lambda x: 'background-color: %s' % 'crimson' if row['600'] > row['Rating']
else 'background-color: %s' % 'orange' if row['600'] < row['Rating'])
我也尝试过这种方法,但没有成功:
def HIGHLIGHT(row):
red = 'background-color: red;'
blue = 'background-color: blue;'
green = 'background-color: green;'
if row['600'] > row['Rating']:
return [red, blue]
elif row['600'] < row['Rating']:
return [blue, red]
else:
return [green, green]
df.style.apply(HIGHLIGHT, subset=['600', 'Rating'], axis=1)
当我这样做时:
ratings = [9,8,3,5,6]
the600 = [10, 6, 5, 2, 1]
df = pd.DataFrame([ratings, the600]).T
df.columns = ['Rating', '600']
def HIGHLIGHT(row):
red = 'background-color: red;'
blue = 'background-color: blue;'
green = 'background-color: green;'
if row['600'] > row['Rating']:
return [red, blue]
elif row['600'] < row['Rating']:
return [blue, red]
else:
return [green, green]
df.style.apply(HIGHLIGHT, subset=['600', 'Rating'], axis=1)
我明白了:
如果它不适合您,我建议您使用 df.dtypes
检查数据类型。例如,如果我按以下方式更改其中一个评级值:
ratings = [9,8,3,"5",6]
我收到这个错误:
TypeError: '>' not supported between instances of 'str' and 'int'
@le-camerone,你的回答帮助我弄清楚发生了什么。我将整个 table 作为浮点值,由于某些原因,这种类型的条件格式在浮点数之间不起作用,但是,在转换为整数后,我能够得到预期的结果。这是我在将浮点数转换为整数的代码之前包含的内容。
df['600'] =pd.to_numeric(df['600'], errors = 'coerce')
df['Rating'] =pd.to_numeric(df['Rating'], errors = 'coerce')
df = df.dropna(subset=['600'])
df = df.dropna(subset=['Rating'])
df['600'] = df['600'].astype(int)
df['Rating'] = df['Rating'].astype(int)
def HIGHLIGHT(row):
red = 'background-color: red;'
blue = 'background-color: blue;'
green = 'background-color: green;'
if row['600'] > row['Rating']:
return [red, blue]
elif row['600'] < row['Rating']:
return [blue, red]
else:
return [green, green]
df.style.apply(HIGHLIGHT, subset=['600', 'Rating'], axis=1)