Display the Pandas DataFrame in table style and border around the table and not around the rows

Let us see how to style a Pandas DataFrame such that it has a border around the table. We will be using the set_table_styles() method of the Styler class in the Pandas module.

set_table_styles()

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 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')]}])

chevron_right


Output :

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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')]}])

chevron_right


Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.