0

Trying to creat formatted data frame and save as pic And want it in special style, But I don't know how.

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)

What I have:

enter image description here

What I want to have:

enter image description here

Does anybody know how to do this?:)

Maybe not exactly like this but with distance between columns/rows

1 Answer 1

1

You can define a CSS style

styles = [dict(selector="td", props=[("border", "5px solid #eee")])]

Then set the style to generate your dataframe at your defined style

df.style.set_table_styles(styles).format(...)

Output with this style:

Sample output

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it helped) BTW how to centrilize column names? and how to do line breaks if column name is too long?
update the styles with the following styles = [dict(selector="td", props=[("border", "5px solid #eee")]), dict(selector="th", props=[("text-align", "center")])]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.