pandas的突出显示(style.highlight方法)
pandas的突出显示(style.highlight方法)
- Pandas提供了一些常用的内置样式,可快速对表格数据进行格式化展示,要记住的是该方法只能在jupyter notebook中显示出来,并不能在pycharm中显示,并且不可用print打印,但是可以将筛选出的文件保存至文件中
空值高亮
- df.style.highlight_null()可对DataFrame中的缺失值进行高亮显示
data = {"Courses":["Numpy","Pandas","Java","Pandas","Python","Python"],"Teacher":["Jack","Lemon","nan","Lemon","Peter","Lemon"],
"Fee":[3000,4500,5000,4000,4900,5000],"Duration":["45days","30days","55days","20days","60days","35days"],"Discount":[200.0,
300.0,100.0,120.0,200.0,"nan"]}
df = pd.DataFrame(data)
df.iloc[1,0] = np.NAN
df.head().style.highlight_null() #head不是必须的,数据框都是可以的
df.style.highlight_null(null_color = "green") #也可通过设置null_color参数进行显示颜色的设置,如设置为green或者blue
最大值与最小值高亮
import pandas as pd
import numpy as np
data = {"Courses":["Numpy","Pandas","Java","Pandas","Python","Python"],"Teacher":["Jack","Lemon","nan","Lemon","Peter","Lemon"],
"Fee":[3000,4500,5000,4000,4900,5000],"Duration":["45days","30days","55days","20days","60days","35days"],"Discount":[200.0,
300.0,100.0,120.0,200.0,1000]}
df = pd.DataFrame(data)
df.style.highlight_max()
#如下图所示:黄色的为该列的最大值,对于字符型,按照26个英文字母的顺序进行排列
df.style.highlight_min()#最小值得显示
#同时显示最大值与最小值并设置不同的颜色!!
a = df.style.highlight_min(color="blue").highlight_max(color="red")
a.to_excel("4月4日杨良实验数据.xlsx")
- 需要注意的是如果在某行或者某列中同时包含“int”和“str”时,会报错,查了些资料说之前的pandas是支持的,后续版本就不支持了(难受,虽然这样做是合理的,但功能我可以不用,你不能越减越少哇!!)
显示某一列或者某几列
df.style.highlight_min(color="blue",subset=["Courses","Teacher"])
- 当然你也懂得可以比较横轴,设置axis=1而已
记录学习的点点滴滴