Pandas How To Uncategorized How to style your dataframe

How to style your dataframe

Styling a dataframe in Pandas allows you to apply various formatting options to the table to make it easier to read and understand. Here are some steps you can take to style your dataframe:

Import the necessary libraries:

import pandas as pd
from IPython.display import display
Create your dataframe:

df = pd.DataFrame({
'Name': ['John', 'Jane', 'Mike', 'Samantha'],
'Age': [23, 45, 32, 28],
'Salary': [40000, 60000, 80000, 50000],

Create a function that defines the styles you want to apply to your dataframe. For example, to highlight the maximum value in each column, you could use the following function:

def highlight_max(s):
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]

Apply the styles to your dataframe using the .style.apply() method. You can pass your style function to the apply() method, along with any other styling options you want to use:

styled_df =
Finally, display the styled dataframe using the display() function from the IPython.display library:


This will display your dataframe with the styling applied. You can modify the style function to apply other formatting options, such as font size, font color, and background color, as well as more complex conditional formatting based on the values in your dataframe.

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post