Jupyter Notebook Python cell format

Author

Nguyễn Ngọc Bình

Format table

import pandas as pd
from IPython.display import HTML
import seaborn as sns

# Load the Iris dataset
iris = sns.load_dataset("iris")

# Define a function to style the header cells and rows with accent color and Tahoma font
def style_dataframe(df, header_background_color="#5F497A", header_text_color="white", content_text_color="black"):
    styles = [
        {"selector": "thead th", "props": [("background-color", header_background_color), ("color", header_text_color), ("font-weight", "bold"), ("font-family", "Tahoma, sans-serif"), ("font-size", "11px")]},
        {"selector": "tbody tr:nth-child(odd)", "props": [("background-color", "#f9f9f9")]},
        {"selector": "tbody tr:nth-child(even)", "props": [("background-color", "white")]},
        {"selector": "tbody td", "props": [("border", "1px solid lightgray"), ("font-family", "Tahoma, sans-serif"), ("font-size", "10px"), ("color", content_text_color)]},
    ]
    return df.style.set_table_styles(styles).hide(axis="index")

# Apply the custom style function
styled_iris = style_dataframe(iris.head(5))

# Display the styled DataFrame using the IPython display function
styled_iris
sepal_length sepal_width petal_length petal_width species
5.100000 3.500000 1.400000 0.200000 setosa
4.900000 3.000000 1.400000 0.200000 setosa
4.700000 3.200000 1.300000 0.200000 setosa
4.600000 3.100000 1.500000 0.200000 setosa
5.000000 3.600000 1.400000 0.200000 setosa