Pandas 以表格样式显示 DataFrame
-
使用
IPython.display模块的display()函数在表格中显示 PandasDataFrame -
使用
tabulate库以表格样式显示 PandasDataFrame -
使用
dataFrame.style在表格中显示 PandasDataFrame -
Pandas 的样式表
DataFrame
Pandas 是一个非常流行和有用的数据科学库。今天,每个参与数据科学的人也广泛使用 Pandas。它以表格形式显示数据,这与我们在 Excel 工具中看到的格式非常相似。使用 excel 工具,我们可以通过添加各种颜色和样式来自定义我们的工作或数据表,使其对其他用户更具吸引力和可读性。通过各种表格样式显示 Pandas DataFrame 增加了数据可视化。
我们将介绍如何使用不同的表格样式以表格的形式显示 Pandas DataFrame,例如 tabulate 库、dataframe.style 和 IPython.display 模块。
使用 IPython.display 模块的 display() 函数在表格中显示 Pandas DataFrame
以表格样式显示 Pandas DataFrame 的最简单和最简单的方法是使用从 IPython.display 模块导入的 display() 函数。此函数以交互式且格式良好的表格形式显示 DataFrame。
请参阅以下示例以更好地理解 display() 函数:
示例代码:
from IPython.display import display
import pandas as pd
# creating a DataFrame
dict = {
"Products": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
display(dataframe)
输出:

使用 tabulate 库以表格样式显示 Pandas DataFrame
使用上述方法,我们可以以有组织的表格样式格式显示 Pandas dataframes。我们将使用一个名为 tabulate 的库。这个库由不同的样式组成,我们可以在其中显示 Pandas dataframes。
在以下示例中,我们将使用 pretty 样式来显示 Pandas DataFrame:
示例代码:
import pandas as pd
from tabulate import tabulate
# creating a DataFrame
dict = {
"Students": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
print(tabulate(dataframe, headers="keys", tablefmt="pretty"))
输出:
+---+--------------------+--------------+-----------------+
| | Students | Price dollar | Percentage Sale |
+---+--------------------+--------------+-----------------+
| 0 | Intel Dell Laptops | 350 | 83 |
| 1 | HP Laptops | 300 | 99 |
| 2 | Lenavo Laptops | 400 | 84 |
| 3 | Acer Laptops | 250 | 76 |
+---+--------------------+--------------+-----------------+
tabulate 库包含以下 styles,我们可以使用它们来为 pandas DataFrame 设置样式:
plainsimplegithubgridfancy_gridpipeorgtbljiraprestoprettypsqlrstmediawikimoinmoinyoutrackhtmllatexlatex_rawlatex_booktabstextile
使用 dataFrame.style 在表格中显示 Pandas DataFrame
我们可以使用 Pandas Style API 以表格样式显示 Pandas DataFrame。我们将在以下代码中使用 dataframe.style。当我们使用 dataframe.style 时,它会返回一个 Styler object,其中包含用于显示 pandas dataframes 的不同格式设置方法。
示例代码:
import pandas as pd
# creating a DataFrame
dict = {
"Students": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
dataframe.style
输出:

Pandas 的样式表 DataFrame
为了增强 Pandas DataFrame 表的样式,我们可以通过与 styler object 链接来使用各种内置函数。
突出显示最大值
请参阅以下示例,其中我们通过与样式器对象链接来使用 highliglight_max() 函数。
示例代码:
import pandas as pd
# creating a DataFrame
dict = {
"Students": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
dataframe.style.highlight_max()
输出:

使用 background_gradient() 函数创建 heatmaps
在下面的示例中,我们通过与样式器对象链接来使用 background_gradient() 函数在 pandas DataFrame 表中创建热图。
示例代码:
import pandas as pd
# creating a DataFrame
dict = {
"Students": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
dataframe.style.background_gradient()
输出:

在 Pandas DataFrame 中设置表属性
我们可以使用 set_properties() 函数增加 Pandas DataFrame 表装饰,如下所示:
示例代码:
import pandas as pd
# creating a DataFrame
dict = {
"Students": ["Intel Dell Laptops", "HP Laptops", "Lenavo Laptops", "Acer Laptops"],
"Price dollar": [350, 300, 400, 250],
"Percentage Sale": [83, 99, 84, 76],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
dataframe.style.set_properties(**{"border": "1.5px solid blue", "color": "red"})
输出:

创建自定义功能
我们还可以将自定义函数与 styler object 一起使用,如下所示:
示例代码:
import pandas as pd
import numpy as np
def table_color(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for less than 60 marks, green otherwise.
"""
color = "green" if val > 60 else "red"
return "color: % s" % color
# creating a DataFrame
dict = {
"Computer Science": [77, 91, 47, 95],
"Statistics": [83, 99, 74, 66],
"English": [71, 67, 40, 55],
}
dataframe = pd.DataFrame(dict)
# displaying the DataFrame
dataframe.style.applymap(table_color)
输出:
