首页 > 其他分享 >pandas的突出显示(style.highlight方法)

pandas的突出显示(style.highlight方法)

时间:2023-04-08 15:45:57浏览次数:34  
标签:style Lemon df Pandas color highlight pandas

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而已

标签:style,Lemon,df,Pandas,color,highlight,pandas
From: https://www.cnblogs.com/yangzilaing/p/17298632.html

相关文章

  • Python Pandas pivot_table 透视表 计数
    pivot_table函数pivot_table(data=表格,index=行,columns=列,values=值,aggfunc=计数函数,margins=True#汇总统计)aggfunc调用函数,不带括号不带括号时,调用的是这个函数本身,是一个函数对象带括号时,调用的是函数的执行结果透视表中......
  • C, cython和pandas dataframe交互int64, int32的选择
    cython调用C代码的一个错误expected'int'butgot'long',原因不复杂,Ccode的int为32bit,而pandasdf缺省为np.int64(64bit),有个参数传递了数组,指针类型就不符了。两个解决方案C代码里面所有相关的int改为longlong类型或者使用pandasdataframe前转换为np.int32,即df.as......
  • Pandas 2.0正式版发布: Pandas 1.5,Polars,Pandas 2.0 速度对比测试
    Pandas2.0正式版在4月3日已经发布了,以后我们pipinstall默认安装的就是2.0版了,Polars是最近比较火的一个DataFrame库,最近在kaggle上经常使用,所以这里我们将对比下Pandas1.5,Polars,Pandas2.0。看看在速度上Pandas2.0有没有优势。PolarsPolars是一个Rust和Python中......
  • Python __ Pandas __ Dataframe 实验课
    基于Dataframe实现以下功能:导入directory.csvimportnumpyasnpimportpandasaspdfdata=pd.read_csv('F:\\directory.csv')dfx=pd.DataFrame(fdata)starbucks=pd.DataFrame(fdata)显示数据集的基本信息print(fdata.head())print(fdata.info())print(fdata.describe(......
  • (数据科学学习手札151)速通pandas2.0新版本干货内容
    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes1简介大家好我是费老师,前两天pandas正式发布了其2.0.0版本,作为一次大版本更新,pandas针对底层进行了大量的重构以优化性能和稳定性,其有关这次更新内容的说明文档更是洋洋洒洒非常繁......
  • 属性指令之class和style
    目录说明classstyle说明class与style本身是属性指令,但是他们比较特殊,应用更广泛。#class:class='变量' 变量可以为:字符串、数组、对象 推荐使用数组,因为class可以使用多个参数#style:style='变量' 变量可以为:字符串、数组、对象 推荐使用对象class#先......
  • python-torch numpy matploit pandas
    title:深度学习基础torchnumpypandasmatplotlibnumpy数组对象是NumPy中最核心的组成部分,这个数组叫做ndarray,是“N-dimensionalarray”的缩写。其中的N是一个数字,指代维度.在NumPy中,数组是由numpy.ndarray类来实现的,它是NumPy的核心数据结构。而Python......
  • highlight 开源全栈监控平台
    highlight开源全栈监控平台,支持error监控,网络监控,session回放,log记录说明对于web录制,目前看是基于了rrwev框架处理的,代码部分使用golang开发,同时提供了不少web框架的支持,值得学习使用下参考资料https://github.com/highlight/highlighthttps://www.highlight.io/docs/......
  • Python __ Pandas
    简介可以看做是Excel是基于Numpy的.优点:处理表格数据(混杂数据)需要引用:importpandasaspdSeries(无用)类似于Numpy的一维数组优点:相较于Nump索引功能强大输出默认带索引:(当为字典是,,默认键是索引)s4=pd.Series([9.53,9.62,9.72])创建时可以指定索引:s5=pd.Series([1,np......
  • pandas dataframe使用方法
    目录基本读写:数据可视化基本读写:使用PandasDataFrame的步骤如下:导入Pandas模块pythonimportpandasaspd创建DataFramepythondf=pd.DataFrame({ '姓名':['张三','李四','王五'], '年龄':[18,25,30], '性别':['男&#......