华拓科技网
您的当前位置:首页Python图表绘制工具:Matplotlib_Part 3

Python图表绘制工具:Matplotlib_Part 3

来源:华拓科技网

####【课程3.13】 表格样式创建     表格视觉样式:Dataframe.style → 返回pandas.Styler对象的属性,具有格式化和显示Dataframe的有用方法

样式创建: ① Styler.applymap:elementwise → 按元素方式处理Dataframe ② Styler.apply:column- / row- / table-wise → 按行/列处理Dataframe

#####样式

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
% matplotlib inline

# 样式

df = pd.DataFrame(np.random.randn(10, 4), columns = ["a", "b", "c", "d"])
sty = df.style
print(sty, type(sty))

sty
复制代码


>#####按元素处理样式:style.applymap() ``` # 按元素处理样式:style.applymap()

def color_neg_red(var): if var < 0: color = "red" else: color = "black" return("color:%s" %color) df.style.applymap(color_neg_red)

创建样式方法,使得小于0的数变成红色

style.applymap() → 自动调用其中的函数

![2.png](https://upload-images.jianshu.io/upload_images/2330091-4ce4a02861e6d2cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<br>


>#####按行/列处理样式:style.apply()
复制代码

按行/列处理样式:style.apply()

def hightlight_max(s): is_max = s == s.max()

print("##",is_max)

lst = []
for v in is_max:
    if v:
        lst.append("background-color: yellow")
    else:
        lst.append("")
return(lst)
复制代码

df.style.apply(hightlight_max, axis = 0, subset = ["b", "c"])

创建样式方法,每列最大值填充黄色

axis:0为列,1为行,默认为0

subset:索引

![3.png](https://upload-images.jianshu.io/upload_images/2330091-805a4ea316c44dde.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<br>


>####样式索引、切片

复制代码

样式索引、切片

df.style.apply(hightlight_max, axis = 1, subset = pd.IndexSlice[2:5, ["b", "d"]])

通过pd.IndexSlice[]调用切片

也可:df[2:5].style.apply(hightlight_max, subset = ["b", "d"]) -> 先索引行再做样式


![4.png](https://upload-images.jianshu.io/upload_images/2330091-3c0a5dde53768718.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<br>





####【课程3.14】  表格显示控制
&ensp;&ensp;&ensp;&ensp;df.style.format()

>##### 按照百分数显示
复制代码

按照百分数显示

df = pd.DataFrame(np.random.randn(10, 4), columns = ["a", "b", "c", "d"]) print(df.head()) df.head().style.format("{:.2%}")



![5.png](https://upload-images.jianshu.io/upload_images/2330091-95819a71b09ddebe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


<br>









>##### 显示小数点数
复制代码

显示小数点数

df.head().style.format("{:.4f}")


![6.png](https://upload-images.jianshu.io/upload_images/2330091-d4917404a752a509.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<br>


>##### 显示正负数
复制代码

显示正负数

df.head().style.format("{:+.2f}")


![7.png](https://upload-images.jianshu.io/upload_images/2330091-a70f59bf8b74f83e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<br>




>####分列显示


复制代码

分列显示

df.head().style.format({"b": "{:.2%}", "c": "{:+.3f}", "d": "{:.3f}"})


![8.png](https://upload-images.jianshu.io/upload_images/2330091-65e5adbdf7595c22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<br>

####【课程3.15】  表格样式调用
&ensp;&ensp;&ensp;&ensp;Styler内置样式调用
>#####定位空值
复制代码

定位空值

df = pd.DataFrame(np.random.rand(5,4), columns = list("ABCD")) df["A"][2] = np.nan df.style.highlight_null(null_color = "red")


![9.png](https://upload-images.jianshu.io/upload_images/2330091-fbef8e1d7fe472a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


<br>
>#####色彩映射
复制代码

色彩映射

df = pd.DataFrame(np.random.rand(10, 4), columns = list("ABCD")) df.style.background_gradient(cmap = "Greens", axis = 1, low = 0, high = 1)

cmap:颜色

axis:映射参考,0为行,1以列


![10.png](https://upload-images.jianshu.io/upload_images/2330091-8622059eaf448f36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


<br>
>##### 条形图
复制代码

条形图

df = pd.DataFrame(np.random.rand(10, 4), columns = list("ABCD")) df.style.bar(subset = ["A", "B", "C"], color = "#d65f5f", width = 100)

width:最长长度在格子的占比


![11.png](https://upload-images.jianshu.io/upload_images/2330091-df6aae785c3281.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

<br>


>####分段式构建样式

复制代码

分段式构建样式

df = pd.DataFrame(np.random.rand(10, 4), columns = list("ABCD")) df["A"][[3,2,1]] = np.nan df.style.bar(subset = ["A", "B"], color = "#d65f5f", width = 100).highlight_null(null_color = "yellow")


![12.png](https://upload-images.jianshu.io/upload_images/2330091-0210cbd6b240a599.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


<br>


#####最后:
[以上完整代码](https://github.com/ChaoRenYuan/Python/tree/master/Python数据分析工具/图表绘制工具:Matplotlib)
复制代码

转载于:https://juejin.im/post/5b0e6a005188253bdb1b3f17

因篇幅问题不能全部显示,请点此查看更多更全内容