Pandas常用操作命令(四)

简介: Pandas常用操作命令(四)

大家好,我是欧K。

本期为大家带来Pandas常用操作命令介绍的第三篇,主要介绍pandas的数据表格样式部分常用到一些指令,本系列会不断进行补充更新希望对你有所帮助。

👉系列文章Pandas常用操作命令(一)
Pandas常用操作命令(二)Pandas常用操作命令(三)


7. 表格样式


示例数据:


以test.csv文件为例:

filename = 'test.csv'
df = pd.read_csv(filename, encoding='gbk')
df

# 重命名列
df.columns = ['姓名','性别','语文','数学','英语','城市','省份']


7.1 设置空值背景红色


df.style.highlight_null(null_color =
'red')


7.2 最大数据高亮


df.style.highlight_max()


7.3 最小数据高亮

df.style.highlight_min()

7.4 部分列最大数据高亮

df.style.apply(highlight_max, subset=['语文',
'数学'])


7.5 部分列数据高亮(Dataframe全为数据)

df3 = df[['语文','数学','英语']]
def highlight_max(s):
    is_max = s == s.max()
    return ['background-color: yellow' if v
else '' for v
in is_max]
df3.style.apply(highlight_max)


7.6 95分以上显示红色


def color_negative_red(val):
    color = 'red' if val > 95.0 else 'black'
    return 'color: %s' % color
df3.style.applymap(color_negative_red)


7.7 混合

df3.style.applymap(color_negative_red).apply(highlight_max)


7.8 设置float类型列数据大于80.0的背景高亮

yellow_css = 'background-color: yellow'
sfun = lambda x: yellow_css if type(x) == float and x > 80.0 else ''
df3.style.applymap(sfun)


7.9 设置数学成绩大于80.0分的行背景高亮

yellow_css = 'background-color: yellow'
sfun = lambda x: [yellow_css]*len(x) if x.数学 > 80.0 else ['']*len(x)
df3.style.apply(sfun, axis=1)


7.10 设置数学成绩大于95.0的行数据颜色为红色

def row_color(s):
    if s.数学 > 95:
        return ['color: red']*len(s)
    else:
        return ['']*len(s)
df3.style.apply(row_color, axis=1)


7.11 显示热度图

import seaborn as sns
cm = sns.light_palette("green", as_cmap=True)
df3.style.background_gradient(cmap=cm)


未完待续。。。


END


以上就是本期为大家整理的全部内容了,赶快练习起来吧,喜欢的朋友可以点赞、点在看也可以分享让更多人知道。

相关文章
|
10月前
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(四)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(四)
|
8月前
|
存储 数据挖掘 索引
【100天精通Python】Day55:Python 数据分析_Pandas数据选取和常用操作
【100天精通Python】Day55:Python 数据分析_Pandas数据选取和常用操作
59 0
|
10月前
|
索引 Python
Pandas常用操作命令(三)
Pandas常用操作命令(三)
|
10月前
|
数据采集 索引 Python
Pandas常用操作命令(二)
Pandas常用操作命令(二)
Pandas常用操作命令(二)
|
10月前
|
SQL 数据采集 JSON
Pandas常用操作命令(一)
Pandas常用操作命令(一)
|
10月前
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(七)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(七)
|
10月前
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(六)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(六)
|
10月前
|
Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(五)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(五)
|
10月前
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(三)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(三)
|
10月前
|
索引 Python
Pandas中DataFrame的属性、方法、常用操作以及使用示例(二)
Pandas中DataFrame的属性、方法、常用操作以及使用示例(二)