Select to view content in your preferred language

Pandas Styler and Borders

1702
0
04-15-2023 11:32 AM
TylerT
by
Occasional Contributor III

Hello,

I would like to add borders to specific cells and when adjacent, border the group without internal borders.  Here is my code so far...

 

import pandas as pd

df = pd.DataFrame({"TransNo":[29, 32, 34, 45, 68, 71, 72, 73, 81],
                    "Type":["Fee", "Sale", "Fee", "Fee", "Sale", "Sale", "Sale", "Sale", "Fee"],
                  "Amount": [1.2, 10.2, 2.1, 3.1, 15.3, 19.2, 100.5, 97.3, 2.2]})

def border(cell_value):
    if cell_value == "Sale":
        return "border: solid thin"
    
df.style.applymap(border)

 

and here is the output...

TylerT_0-1681583278221.png

 

and here is the markup of the desired output....

TylerT_1-1681583415694.png

The red markup indicating the deletion of internal lines.  Any pointers would be appreciated.  Thank you.  

Tyler

0 Replies