如何在 Pandas 数据框中的指定列上显示条形图?
在本文中,我们将了解如何在数据框中的指定列上显示条形图。为了完成这项任务,我们使用 Pandas Dataframe 的DataFrame.style.bar()方法。
Syntax: pandas.DataFrame.style.bar(columns_list, color)
Return: Dataframe with the given color bar strips on the positive definite values. The None value and the negative values are skipped in the process.
现在,让我们创建一个数据框:
Python3
# importing required packages
import pandas
import numpy
Nan = numpy.nan
# creating a dataframe with some nan values
df = pandas.DataFrame(data = numpy.array(
[[9, -3.4, Nan, 9, Nan],
[9, 4.89, -3.4, Nan, 9],
[Nan, -3.4, Nan, 9, 56],
[4, -3.4, 59.0, 5.6, 90],
[-4, Nan, Nan, 34, 8.8]]),
columns = list("ABCDE"))
# view dataframe
df
Python3
# display the bar chart on dataframe
df.style.bar(subset = ['B', 'C'],
color = 'skyblue')
Python3
# display the bar chart on dataframe
df.style.bar(subset = ['A', 'D', 'E'],
color = 'yellow')
输出:
示例 1:
Python3
# display the bar chart on dataframe
df.style.bar(subset = ['B', 'C'],
color = 'skyblue')
输出:
示例 2:
Python3
# display the bar chart on dataframe
df.style.bar(subset = ['A', 'D', 'E'],
color = 'yellow')
输出: