In the following article, we will explore a method to add colors and styles to Pandas DataFrames.
In the following section of this article, we will explore a method to add colors and styles to Pandas DataFrames. Our focus will be on the application of colors and emojis, utilizing approaches similar to the popular conditional formatting commonly employed in pivot tables within spreadsheets. Through this strategy, we aim to enhance the presentation of our data, making the exploration and understanding of information not only informative but also visually appealing
Pandas Styler is a module within the Pandas library that provides methods for creating HTML-styled representations of DataFrames. This feature allows for the customization of the visual appearance of DataFrames during their visualization. The core functionality of Pandas Styler lies in the ability to highlight, color, and format cells based on specific conditions, facilitating the visual identification of patterns and trends in datasets.
Also, Pandas Styler stands out for its capability to assist in the design of DataFrames or series by generating visual representations using HTML and CSS. This functionality simplifies the creation of attractive and customized data presentations, enhancing the visualization experience, and enabling a more intuitive interpretation of the information contained in the datasets.
Next we have the code with we are going to create a pivot table using a set of data and from this you will begin to give it different styles and conditional formats such as can be seen in the previous image.
The pivot table is a tabular data structure that provides a summarized overview of information from another table, organizing the data based on one variable and displaying values associated with another variable. In this specific scenario, the pivot table organizes the data according to the βsmokerβ column and presents the total sum of tips, categorized by the days on which clients consume in the restaurant
The following example shows the pivot_table
method with the βtipsβ DataFrame
python code
import pandas as pd
import seaborn as sns
# create the tips dataframe
= sns.load_dataset('tips')
data = pd.pivot_table(data,
data_pivot ='smoker',
index='day',
columns='total_bill',
values='sum').reset_index()
aggfunc data_pivot
ouput
day | smoker | Thur | Fri | Sat | Sun |
---|---|---|---|---|---|
0 | Yes | 326.24 | 252.20 | 893.62 | 458.28 |
1 | No | 770.09 | 73.68 | 884.78 | 1168.88 |
In this analysis, we will use the βπ Apple Store appsβ DataFrame to explore the creation of pivot tables and customization of table styles. This dataset provides detailed insights into Apple App Store applications, covering aspects from app names to specifics like size, price, and ratings. Our objective is to efficiently break down the information while applying styles that enhance the presentation and comprehension of data effectively.
The dataset was downloaded from Kaggle and it contains more than 7000 Apple iOS mobile application details. It is important to note that the data was collected in July 2017.
column_name | Β column description |
---|---|
track_name | the column contains the name of the app. |
size_bytes | the column contains the size of the app in bytes. |
currency | the column contains the currency type. |
price | the column contains the price of the app. |
rating_count_tot | the column contains the total number of ratings. |
rating_count_ver | the column contains the number of ratings for the current version of the app. |
user_rating | the column contains the average user rating for the app. |
user_rating_ver | the column contains the average user rating for the current version of the app. |
ver | the column contains the current version of the app. |
cont_rating | the column contains the content rating. |
prime_genre | the column contains the primary genre. |
sup_devices.num | the column contains the number of supported devices. |
ipadSc_urls.num | the column contains the number of screenshots showed for display. |
lang.num | the column contains the number of supported languages. |
vpp_lic | the column contains the Vpp Device Based Licensing Enabled. |
In the following code chunk, we will create a DataFrame by reading the CSV file.
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
import warnings
# Deactivate pandas warning
'ignore')
warnings.filterwarnings(
print("Python Libraries version:")
print('--'*20)
print("Pandas version: ", pd.__version__)
print("Numpy version: ", np.__version__)
print("Matplotlib version: ", plt.matplotlib.__version__)
Python Libraries version:
----------------------------------------
Pandas version: 2.1.3
Numpy version: 1.26.1
Matplotlib version: 3.8.1
# Create a dataframe from a csv file
# You can download the file from the following link https://github.com/r0mymendez/pandas-styles
='data/AppleStore.csv'
path=pd.read_csv(path,sep=';') data
In the next step, we are going to create a pivot table from a DataFrame.
# Pivot table
# filter the data to keep only the top 15 genres
= data.value_counts('prime_genre')[:15].index.tolist()
top_genre = data.loc[data['prime_genre'].isin(top_genre),['prime_genre','user_rating','price']]
tmp
# create a new column with the rating rounded to the nearest integer
'user_rating'] = [f'rating_{str(math.trunc(item))}' for item in tmp['user_rating']]
tmp[
# create a pivot table
= (
tmp_pivot
pd.pivot_table(= tmp,
data ='user_rating',
columns='prime_genre',
index='price',
values='mean',
aggfunc=0
fill_valueround(2)
).reset_index().
)# rename the columns
=''
tmp_pivot.columns.name# print the pivot table
tmp_pivot
Now we will explore the style
module in Pandas, that enables us to enhance the visual presentation of DataFrames.
The style
module provides a differents of options to modify the appearance of the data, allowing us to customize aspects such as:
In this section, we will apply styles to both the titles and the table. Therefore we use background colors to highlight the headers and the rest of the table.
# Styling: Changing Background Color for Column Headers
= {
headers 'selector': 'th.col_heading',
'props': 'background-color: #5E17EB; color: white;'
}
= {
index_style 'selector': 'th.index_name',
'props': 'background-color: #5E17EB; color: white;'
}
= (
tmp_pivot_style
tmp_pivot
.style
.set_table_styles([headers,index_style])**{'background-color': '#ECE3FF','color': 'black'})
.set_properties(
)
tmp_pivot_style
In following code snippet illustrates how to set a custom background color for a particular cell in our DataFrame using pandas styling.
(
tmp_pivot
.style
.set_table_styles([headers, index_style])**{'background-color': '#ECE3FF', 'color': 'black'})
.set_properties(**{'background-color': '#FD636B', 'color': 'white'},subset=pd.IndexSlice[4, 'rating_5'])
.set_properties( )
Now, we will focus on highlighting the maximum and minimum values in our DataFrame. For this reason, we will assign distinctive background colors to these extreme values, facilitating a quicker and more intuitive understanding of the dataset. The code snippet below demonstrates how to implement this stylistic enhancement.
#Β select the columns that start with 'rating_'
= tmp_pivot.columns[tmp_pivot.columns.str.startswith('rating_')]
columns
#Β get the max and min values
= tmp_pivot[columns].max().max()
max_value = tmp_pivot[columns].min().min()
min_value
# Establecer el estilo para la celda con el valor mΓ‘ximo
= f'border: 4px solid #3BE8B0 !important;'
max_style
# Establecer el estilo para la celda con el valor mΓnimo
= f'background-color: #FF66C4; '
min_style
(
tmp_pivot
.style
.set_table_styles([headers, index_style])**{'background-color': '#ECE3FF', 'color': 'black'})
.set_properties(**{'background-color': '#FD636B', 'color': 'white'}, subset=pd.IndexSlice[4, 'rating_5'])
.set_properties(lambda x: max_style if x == max_value else '')
.applymap(lambda x: min_style if x == min_value else '', subset=columns)
.applymap( )
In the upcoming section, we will delve into the concept of color maps, representing a spectrum of colors arranged in a gradient. A colormap, essentially a palette of colors, consists of distinctive denominations, with the most popular ones being [βviridis,β βmagma,β βGreens,β βRedsβ].
The primary objective behind creating these color spectrums is to enhance the visual representation of data. Each color in the gradient carries specific nuances, contributing to a more nuanced data visualization experience.
For an extensive array of color options, you can explore the matplotlib colormaps link.
import matplotlib.pyplot as plt
import numpy as np
# Define the colormap
for cmap_item in ['viridis', 'magma','Greens','Reds']:
= plt.get_cmap(cmap_item)
cmap # Create a color gradient
= np.linspace(0, 1, 256).reshape(1, -1)
gradient
# Display the color palette
=(10, 0.2))
plt.figure(figsize='auto', cmap=cmap)
plt.imshow(gradient, aspect'off')
plt.axis(f'{cmap_item.capitalize()} Color Palette', loc='left', fontsize=9)
plt.title( plt.show()
Viridis palette
Now, we will apply a color gradient to our pivot table, allowing you to observe how it is colored using the Viridis palette. In this context, lighter colors signify larger values within the distribution, while darker shades correspond to smaller values in the distribution. This approach provides a visual representation that intuitively conveys the magnitude of the data, making it easier to discern patterns and variations across the dataset.
'viridis',lut=20) plt.get_cmap(
(
tmp_pivot
.style
.set_table_styles([headers, index_style])='viridis',subset=columns)
.background_gradient(cmap )
In the next code chunk, we will enhance the visual representation of our pivot table by introducing distinct color backgrounds to specific columns. This technique aids in better highlighting and categorizing data, making it easier to draw insights from the table.
(
tmp_pivot
.style
.set_table_styles([headers, index_style])**{'background-color': '#FFCFC9','color':'black'},subset=['rating_0','rating_1'])
.set_properties(**{'background-color': '#FFF1B0','color':'black'},subset=['rating_2','rating_3'])
.set_properties(**{'background-color': '#BEEAE5','color':'black'},subset=['rating_4','rating_5'])
.set_properties( )
In this section, we will implement the style.bar function to introduce a dynamic color bar into our DataFrame. The color bar provides a visual representation of data values, assigning varying colors to different data ranges.
(
tmp_pivot
.style
.set_table_styles([headers, index_style])**{'background-color': '#ECE3FF', 'color': 'black'})
.set_properties(**{'background-color': 'white','color':'black'},subset=columns)
.set_properties(='#FFCFC9',subset=['rating_0','rating_1'])
.bar(color='#FFF1B0',subset=['rating_2','rating_3'])
.bar(color='#BEEAE5',subset=['rating_4','rating_5'])
.bar(color )
In this section, we explore the enhancement of data representation by adding an image to an additional column. This approach provides an alternative method to elevate the visual impact of the data being presented. These images can serve as icons, represent brands, or convey additional visual elements to captivate and engage the audience.
# create a function to add an image to the dataframe depending on the genre
def add_image(image_name):
= f"img/icons/img_{image_name}.png"
img_url = "width: 50px"
width = "height: 50px"
height ="center"
text_align return f'{width};{height}; content: url({img_url}); text-align:{text_align}'
# apply the function to the dataframe
= (
styled_df
tmp_pivot5)
.head(
.reset_index()'index': 'genre'}, axis=1)
.rename({=pd.IndexSlice[:, ['genre']])
.style.applymap(add_image, subset
.set_table_styles([headers, index_style])**{'background-color': '#ECE3FF', 'color': 'black'})
.set_properties(
)
# display the dataframe with the images
display(styled_df)
Disclaimer: Issues with Notebook Cache
During the creation of this content, I encountered difficulties related to the notebook cache. Despite making changes to the images, the visualization did not update correctly. Even after attempting to restart the kernel and clear the cell output, the problem persisted. The only effective solution I found was to change the file names of the images, thus avoiding unexpected cache behavior.
Itβs important to note that these issues may be specific to the Jupyter Notebooks environment and may not reflect inherent limitations in the code or libraries used. While I tried to address this problem, I did not find a complete solution and opted for an alternative fix by changing the file names.
If you have suggestions or additional solutions, I would be delighted to learn and improve this process.
In this section, we delve into the creative use of emojis based on percentile values, offering a distinctive approach to elevate data representation. By incorporating diverse emojis, we enhance the visual impact of the data. Specifically, we employ circles and squads as emojis to bring nuanced expressions to our data points.
If youβd like to view the code for creating this style, itβs available in my GitHub repository. Feel free to check it out and give it a star if you find it helpful! βοΈ
If you want to learnβ¦ * πΌ Pandas Style Documentation
Other references: * Image preview reference: [Imagen de vectorjuice en Freepik]
Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Mendez (2024, Jan. 2). Romina Mendez: Transform your Pandas Dataframes: Styles, π¨ Colors, and π Emojis. Retrieved from https://r0mymendez.github.io/posts_en/2024-01-02-transform-your-pandas-dataframes/
BibTeX citation
@misc{mendez2024transform, author = {Mendez, Romina}, title = {Romina Mendez: Transform your Pandas Dataframes: Styles, π¨ Colors, and π Emojis}, url = {https://r0mymendez.github.io/posts_en/2024-01-02-transform-your-pandas-dataframes/}, year = {2024} }