在Python中设置Pandas数据框的背景颜色和字体颜色
正如我们所知,造型的基本思想是为了使最终用户的可读性更有影响力。我们可以对数据的颜色和格式进行修改,以便更有效地传达洞察力。为了在pandas DataFrame上实现更有冲击力的可视化,一般来说,我们可以使用DataFrame.style属性,该属性会返回styller对象,该对象有许多有用的方法来格式化和可视化数据框架。
使用DataFrame.style属性
- df.style.set_properties。通过使用这个,我们可以使用内置的功能来操作数据框架的样式,从字体颜色到背景颜色。
# Importing the necessary libraries -->
import pandas as pd
import numpy as np
# Seeding random data from numpy
np.random.seed(24)
# Making the DataFrame
df = pd.DataFrame({'A': np.linspace(1, 10, 10)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4),
columns=list('BCDE'))], axis=1)
# DataFrame without any styling
print("Original DataFrame:\n")
print(df)
print("\nModified Stlying DataFrame:")
df.style.set_properties(**{'background-color': 'black',
'color': 'green'})
输出:
df.style.set_properties
- df.style.highlight_null : 在它的帮助下,我们可以突出显示数据框架中的缺失或空值。
# Replacing the locating value by NaN (Not a Number)
df.iloc[0, 3] = np.nan
df.iloc[2, 3] = np.nan
df.iloc[4, 2] = np.nan
df.iloc[7, 4] = np.nan
# Highlight the NaN values in DataFrame
print("\nModified Stlying DataFrame:")
df.style.highlight_null(null_color='red')
输出:
df.style.highlight_null
- df.style.highlight_min : 用于突出显示整个数据框架中每一列的最小值。
# Highlight the Min values in each column
print("\nModified Stlying DataFrame:")
df.style.highlight_min(axis=0)
输出:
df.style.highlight_min
- df.style.highlight_max :用于突出显示整个数据框架中每一列的最大值。
# Highlight the Max values in each column
print("\nModified Stlying DataFrame:")
df.style.highlight_max(axis=0)
输出:
df.style.highlight_max
使用用户定义的函数
- 我们可以使用一个用户定义的函数来修改DataFrame。在这个函数的帮助下,我们可以自定义数据框内正数据值的字体颜色。
# function for set text color of positive
# values in Dataframes
def color_positive_green(val):
"""
Takes a scalar and returns a string with
the css property `'color: green'` for positive
strings, black otherwise.
"""
if val > 0:
color = 'green'
else:
color = 'black'
return 'color: %s' % color
df.style.applymap(color_positive_green)
输出:
User-Defined Function
使用Seaborn图书馆
- 在DataFrame中使用调色板进行梯度填充。通过从seaborn库中导入浅色调色板,我们可以为数据框的背景绘制颜色梯度。
# Import seaborn library
import seaborn as sns
# Declaring the cm variable by the
# color palette from seaborn
cm = sns.light_palette("green", as_cmap=True)
# Visualizing the DataFrame with set precision
print("\nModified Stlying DataFrame:")
df.style.background_gradient(cmap=cm).set_precision(2)
输出:
Seaborn调色板
- 使用调色板来突出空值或缺失值。这里,我们用Seaborn的梯度调色板以红色突出NaN值。
# Highlight the NaN values in DataFrame
# using seaborn color palette
print("\nModified Stlying DataFrame:")
df.style.background_gradient(cmap=cm).set_precision(2).highlight_null('red')
输出:
Seaborn调色板与highlight_null。
- 用DataFrame.style属性组装Seaborn属性。用数据框架的高亮属性定制seaborn调色板,以实现更有影响力的数据可视化。
# Highlight the NaN values in DataFrame
# using seaborn color palette as well as
# min('lighblue') and max('blue') values
# in each column
print("\nModified Stlying DataFrame:")
df.style.background_gradient(cmap=cm).set_precision(2).highlight_null('red').highlight_min(axis=0, color='lightblue').highlight_max(axis=0, color='blue')
输出:
Seaborn调色板具有不同的高光属性。