获取想要设置格式的数据
import pandas as pd
df=pd.read_csv("C:\\Users\\Desktop\\xz.csv",encoding='GBK',header=None)
df1=df.loc[:,1:5]
df1.columns=['xc','xr','xx','cx','sq']
import numpy as np
df1.loc[4:6,['xc']]=np.nan
#呈现条形图
df1.style.bar("xx",vmin=0)
def negative_color_red(val):
color='red' if val < 0 else 'black'
return 'color: %s'% color
df1.style.applymap(negative_color_red) ##显示负数
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
df1.style.apply(highlight_max)
4、空值背景色为红色
df1.style.highlight_null()
#df1.style.highlight_null(null_color='green') ##背景色变绿色
5、指定列的数值为负值,字体变红色
df1.style.applymap(color_negative_red,
subset=pd.IndexSlice[2:5, ['cx', 'sq']])
6、背景色呈现阶梯变化
#背景颜色呈现阶梯变化
df1.style.background_gradient("Greens",subset="xr")
9、数据小于4位填充0,加个+号及2位数
df1.style.format({'xx': "{:0<4.0f}", 'cx': '{:+.2f}'})
df1.style.format({"sq": lambda x: "±{:.2f}".format(abs(x))})
链接:https://pandas.pydata.org/pandas-docs/version/0.18/style.html