Home » Tutorials » Python Tutorials » Python – Display the Pandas DataFrame in Table Style and Border Around the Table

Python – Display the Pandas DataFrame in Table Style and Border Around the Table

Let us see How to Display the Pandas DataFrame in Table Style and Border Around the Table and Not Around the Rows. We will be using the set_table_styles() method of the Styler class in the Pandas module.

Syntax : set_table_styles(self, table_styles)

Parameters :
table_styles : List, each individual table_style should be a dictionary with selector and props keys.

Returns : Styler

Example 1 :

# import the module
import pandas as pd
# create a DataFrame
ODI_runs = {'name': ['Tendulkar', 'Sangakkara', 'Ponting',
'Jayasurya', 'Jayawardene', 'Kohli',
'Haq', 'Kallis', 'Ganguly', 'Dravid'],
'runs': [18426, 14234, 13704, 13430, 12650,
11867, 11739, 11579, 11363, 10889]}
df = pd.DataFrame(ODI_runs)
# making a yellow border
df.style.set_table_styles([{'selector' : '',
'props' : [('border',
'10px solid yellow')]}])

Output :
Display the Pandas DataFrame in Table Style and Border Around the Table - myTechMint

Example 2 :

# import the module
import pandas as pd
# create a DataFrame
df = pd.DataFrame({"A":[14, 4, 5, 4, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 20, 7, 3, 8],
"D":[14, 3, 6, 2, 6]})
# making a green border
df.style.set_table_styles([{'selector' : '',
'props' : [('border',
'2px solid green')]}])

Output :
Display the Pandas DataFrame in Table Style and Border Around the Table - myTechMint

Leave a Comment