Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
Styling a pandas DataFrame in a Jupyter Notebook can make data visualization more engaging and readable. The styling is achieved using CSS, and pandas provides a simple interface to apply styles to DataFrames.
Here's a tutorial on how to add CSS to a pandas DataFrame in a Jupyter Notebook:
Start by setting up your Jupyter Notebook environment and create a sample DataFrame:
import pandas as pd import numpy as np np.random.seed(24) df = pd.DataFrame({'A': np.linspace(1, 10, 10)}) df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))], axis=1) df.iloc[3, 3] = np.nan df
Use the style
property of the DataFrame:
df.style
You can add a color gradient to highlight values. This can be particularly useful for heatmaps.
df.style.background_gradient(cmap='viridis')
Highlight the max value in each column:
df.style.highlight_max(axis=0)
Or the min:
df.style.highlight_min(axis=0)
You can add custom CSS to a DataFrame using the set_table_styles
method:
styles = [ dict(selector="th", props=[("font-size", "150%"), ("text-align", "center")]), dict(selector="td", props=[("font-size", "120%"), ("text-align", "center")]) ] df.style.set_table_styles(styles)
Styles can be chained together:
df.style.highlight_max(axis=0).background_gradient(cmap='Blues')
You can include bars in your DataFrame for quick visualizations:
df.style.bar(subset=['A', 'B'], align='mid', color=['#d65f5f', '#5fba7d'])
The pandas styling interface provides a wide range of possibilities, from basic color mapping to more advanced CSS. While it doesn't replace full-fledged data visualization tools, it's a handy and quick way to make your DataFrame visually appealing directly within Jupyter Notebooks. Remember, these styles are primarily for visualization in the notebook and won't carry over if you export the DataFrame to other formats like CSV.
Jupyter Notebook custom styling for Pandas DataFrame:
import pandas as pd from IPython.display import display, HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom CSS styles styles = """ th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } """ # Display DataFrame with custom styles display(HTML('<style>{}</style>'.format(styles))) display(df)
Adding CSS styles to Pandas DataFrame output in Jupyter Notebook:
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom CSS styles styles = { 'background-color': 'lightblue', 'color': 'darkred', 'font-weight': 'bold', } # Apply styles to DataFrame df.style.set_properties(**styles)
HTML and CSS styling in Jupyter Notebook with Pandas:
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom HTML and CSS styling styles = """ <style> th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Display DataFrame with custom styles display(HTML(styles)) display(df)
Jupyter Notebook Markdown cell with Pandas and CSS:
import pandas as pd from IPython.display import display, Markdown # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom Markdown cell with CSS styling markdown_styles = """ <style> th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Display DataFrame with custom styles in a Markdown cell display(Markdown(markdown_styles)) display(df)
Applying custom styles to Pandas DataFrame in Jupyter Notebook:
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom styling function def custom_style(value): return 'background-color: lightblue; color: darkred; font-weight: bold;' # Apply custom styles to DataFrame df.style.applymap(custom_style)
Jupyter Notebook display options for Pandas with CSS:
import pandas as pd from IPython.display import display, HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Set display options with custom CSS pd.set_option('display.notebook_repr_html', True) pd.set_option('display.max_colwidth', 20) pd.set_option('display.max_columns', 5) # Custom CSS styles styles = """ <style> th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Display DataFrame with custom styles display(HTML(styles)) display(df)
HTML and CSS in Jupyter Notebook for visualizing Pandas DataFrames:
import pandas as pd from IPython.display import display, HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom HTML and CSS styling styles = """ <style> th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Display DataFrame with custom styles display(HTML(styles + df.to_html()))
Embedding CSS styles in Jupyter Notebook cells with Pandas:
import pandas as pd from IPython.core.display import HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom CSS styles styles = """ th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } """ # Display DataFrame with embedded CSS styles HTML(df.style.set_table_styles([{'selector': 'thead', 'props': [('background-color', '#f2f2f2')]}, {'selector': 'td', 'props': [('font-weight', 'bold'), ('color', 'blue')]}]).render())
Styling options for Pandas DataFrame in Jupyter Notebook:
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom styling using the Styler object styler = df.style \ .set_properties(**{'background-color': 'lightblue', 'color': 'darkred', 'font-weight': 'bold'}) \ .set_table_styles([{'selector': 'thead', 'props': [('background-color', '#f2f2f2')]}]) # Display styled DataFrame styler
Customizing Pandas DataFrame output appearance in Jupyter Notebook:
import pandas as pd # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom styling using the Styler object styler = df.style \ .set_properties(**{'background-color': 'lightblue', 'color': 'darkred', 'font-weight': 'bold'}) \ .set_table_styles([{'selector': 'thead', 'props': [('background-color', '#f2f2f2')]}]) # Display styled DataFrame styler
Combining HTML, CSS, and Pandas for enhanced visualization in Jupyter:
import pandas as pd from IPython.display import display, HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom HTML and CSS styling styles = """ <style> th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Display DataFrame with custom styles display(HTML(styles + df.to_html()))
Using Markdown cells for CSS styling in Jupyter Notebook with Pandas:
import pandas as pd from IPython.display import Markdown # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom Markdown cell with CSS styling markdown_styles = """ <style> th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Display DataFrame with custom styles in a Markdown cell display(Markdown(markdown_styles)) display(df)
Advanced Pandas DataFrame styling in Jupyter Notebook:
import pandas as pd from IPython.display import display, HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom advanced styling using the Styler object styler = df.style \ .bar(subset=['A'], color='lightblue') \ .bar(subset=['B'], color='lightgreen') \ .set_properties(**{'background-color': 'lightyellow', 'color': 'darkred', 'font-weight': 'bold'}) \ .set_table_styles([{'selector': 'thead', 'props': [('background-color', '#f2f2f2')]}]) # Display styled DataFrame styler
Visualizing Pandas DataFrames with custom CSS styles in Jupyter:
import pandas as pd from IPython.display import display, HTML # Sample DataFrame df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]}) # Custom CSS styles for visualization styles = """ <style> .highlight_max { background-color: lightgreen !important; color: darkred !important; } th { background-color: #f2f2f2; } td { font-weight: bold; color: blue; } </style> """ # Highlight maximum values and display DataFrame with custom styles display(HTML(styles)) display(df.style.highlight_max(axis=0))