Saturday, November 16, 2024
Google search engine
HomeLanguagesDisplay the Pandas DataFrame in table style and border around the table...

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 :




# 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 :

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 :

RELATED ARTICLES

Most Popular

Recent Comments