試圖創建格式化的資料框并保存為圖片并希望它具有特殊的風格,但我不知道如何。
import pandas as pd
import dataframe_image as dfi
from matplotlib.colors import LinearSegmentedColormap
d = {'index': ['23.01.2022', '22.01.2022','21.01.2022','20.01.2022','total'],
'col1': [1,2,3,4,10],
'col2': [100,200,300,400,1000],
'col3': [10,20,30,40,100],
}
df = pd.DataFrame(data=d).set_index('index')
c = ["red","tomato","coral","lightcoral","yellow", "palegreen","green","darkgreen"]
v = [0,.15,.3,.4,.5,0.6,.9,1.]
l = list(zip(v,c))
cmap=LinearSegmentedColormap.from_list('rg',l, N=256)
test = df.style.format({"col1": "{:20,.0f}",
"col2": "{:20,.0f}",
"col3":"{0:.1f}%"})\
.hide_index()\
.bar(subset=pd.IndexSlice[df.index!='total',["col2",]], color='#FFA07A')\
.background_gradient(cmap=cmap,subset=pd.IndexSlice[df.index!='total',['col1','col3',]])\
.set_properties(**{'text-align': 'center'})
dfi.export(test,img_path)
我有的:

我想擁有的:

有人知道怎么做這個嗎?:)
也許不完全像這樣,但列/行之間的距離
uj5u.com熱心網友回復:
您可以定義 CSS 樣式
styles = [dict(selector="td", props=[("border", "5px solid #eee")])]
然后設定樣式以按照您定義的樣式生成資料框
df.style.set_table_styles(styles).format(...)
這種風格的輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/421089.html
標籤:
上一篇:熊貓用序號重命名選定的列
下一篇:合并第一行的單元格
