在 Pandas Dataframe 中突出显示负值红色和黑色正值
让我们看看在 Pandas Dataframe 中突出显示正值红色和黑色负值的各种方法。
首先,让我们制作一个数据框:
Python3
# Import Required Libraries
import pandas as pd
import numpy as np
# Create a dictionary for the dataframe
dict = {
'Name': ['Sukritin', 'Sumit Tyagi',
'Akriti Goel', 'Sanskriti',
'Abhishek Jain'],
'Age': [22, 20, 45, 21, 22],
'Marks': [90, 84, -33, -87, 82]
}
# Converting Dictionary to
# Pandas Dataframe
df = pd.DataFrame(dict)
# Print Dataframe
print(df)
Python3
# Define a function for colouring
# negative values red and
# positive values black
def highlight_max(s):
if s.dtype == np.object:
is_neg = [False for _ in range(s.shape[0])]
else:
is_neg = s < 0
return ['color: red;' if cell else 'color:black'
for cell in is_neg]
# Using apply method of style
# attribute of Pandas DataFrame
df.style.apply(highlight_max)
Python3
# Define a function which
# returns the list for
# df.style.apply() method
def highlight_max(s):
if s.dtype == np.object:
is_neg = [False for _ in range(s.shape[0])]
else:
is_neg = s < 0
return ['background: red; color:white'
if cell else 'background:black; color:white'
for cell in is_neg]
# Using apply method of style
# attribute of Pandas DataFrame
df.style.apply(highlight_max)
Python3
# Define a function for
# colouring negative values
# red and positive values black
def highlight_max(cell):
if type(cell) != str and cell < 0 :
return 'color: red'
else:
return 'color: black'
df.style.applymap(highlight_max)
Python3
# Define a function which
# returns string for
# applymap() method
def highlight_max(cell):
if type(cell) != str and cell < 0 :
return 'background: red; color:black'
else:
return 'background: black; color: white'
df.style.applymap(highlight_max)
输出:
现在,来到突出显示部分。我们的目标是突出显示红色的负值和黑色的正值。
方法 1:使用Dataframe.style.apply() 。
Syntax: DataFrame.style.apply(self, func, axis=0, subset=None, **kwargs)
Parameters:
- func: It should take a pandas.Series or pandas.DataFrame based on the axis and should return an object with the same shape.
- axis: {0 or ‘index’, 1 or ‘columns’, None}, default 0. Apply to each column (axis=0 or ‘index’), to each row (axis=1 or ‘columns’), or to the entire DataFrame at once with axis=None.
- subset: Set of columns or rows on which you want to call the func.
- **kwargs: Pass along to func.
Returns: Styler object.
示例 1:突出显示文本。
Python3
# Define a function for colouring
# negative values red and
# positive values black
def highlight_max(s):
if s.dtype == np.object:
is_neg = [False for _ in range(s.shape[0])]
else:
is_neg = s < 0
return ['color: red;' if cell else 'color:black'
for cell in is_neg]
# Using apply method of style
# attribute of Pandas DataFrame
df.style.apply(highlight_max)
输出:
示例 2:突出显示单元格而不是文本。
Python3
# Define a function which
# returns the list for
# df.style.apply() method
def highlight_max(s):
if s.dtype == np.object:
is_neg = [False for _ in range(s.shape[0])]
else:
is_neg = s < 0
return ['background: red; color:white'
if cell else 'background:black; color:white'
for cell in is_neg]
# Using apply method of style
# attribute of Pandas DataFrame
df.style.apply(highlight_max)
输出:
方法 2:使用dataframe.style.applymap()方法。
Syntax: DataFrame.style.applymap(self, func, subset=None, **kwargs)
Parameters:
- func: It takes a scalar value and return the scalar values
- subset: Set of columns or rows on which you want to call the func.
- **kwargs: Pass along to func.
Returns: Styler object.
示例 1:突出显示文本。
Python3
# Define a function for
# colouring negative values
# red and positive values black
def highlight_max(cell):
if type(cell) != str and cell < 0 :
return 'color: red'
else:
return 'color: black'
df.style.applymap(highlight_max)
输出:
示例 2:突出显示单元格而不是文本。
Python3
# Define a function which
# returns string for
# applymap() method
def highlight_max(cell):
if type(cell) != str and cell < 0 :
return 'background: red; color:black'
else:
return 'background: black; color: white'
df.style.applymap(highlight_max)
输出:
注意: pandas.DataFrame.applymap() 方法只将单个单元格传递给可调用函数,而 pandas.DataFrame.apply() 将 pandas.Series 传递给可调用函数。
参考: Pandas 中的样式