âčïž Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.8 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://likegeeks.com/nan-values-seaborn-heatmap/ |
| Last Crawled | 2026-03-22 10:17:11 (23 days ago) |
| First Indexed | 2024-02-08 17:08:05 (2 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Handle NaN values in Seaborn heatmap |
| Meta Description | Learn how to handle NaNs in Seaborn heatmaps with this tutorial. Learn to drop, fill, interpolate, and visually distinguish NaNs in real-world data. |
| Meta Canonical | null |
| Boilerpipe Text | One common challenge faced when creating heatmaps is the presence of NaN (Not a Number) values.
These NaNs can impact the visual output, leading to misleading interpretations or an unclear representation of the data.
In this tutorial, youâll learn how to handle NaN values in
Seaborn heatmaps
.
Whether you are dealing with sparse NaNs that can be easily dropped, or require more nuanced approaches like filling, interpolating, or visually distinguishing NaNs, this tutorial covers it all.
1
Remove NaN Values
2
Fill NaNs with a Specific Value
3
Interpolate NaNs
4
Mask NaNs in the Heatmap
5
Use a Different Color for NaNs
Remove NaN Values
In scenarios where NaNs are sparse and not critical to your analysis, one effective method is to remove them using the
dropna()
method.
First, youâll need to import the necessary libraries and create a sample dataset.
import seaborn as sns
import pandas as pd
import numpy as np
data = {
'Call Quality': [3.5, np.nan, 4.2, 3.8],
'Internet Speed': [50, 45, np.nan, 60],
'Customer Satisfaction': [4.0, 3.6, np.nan, 4.5]
}
df = pd.DataFrame(data)
print(df)
Output:
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
1 NaN 45.0 3.6
2 4.2 NaN NaN
3 3.8 60.0 4.5
Note the NaN values in the dataset.
Next, letâs remove these NaN values:
cleaned_df = df.dropna()
print(cleaned_df)
Output:
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
3 3.8 60.0 4.5
In the above step,
dropna()
removes rows with any NaN values.
Letâs plot the heatmap using Seaborn after weâve dropped the NaN values.
This will help visualize how the removal of NaN values impacts the heatmap representation.
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
sns.heatmap(cleaned_df, annot=True, cmap='viridis')
plt.title('Heatmap (Without NaN Values)')
plt.show()
Output:
Fill NaNs with a Specific Value
In certain situations, you want to retain the original size of your dataset, especially when the presence of each row is significant for your analysis.
In such cases, rather than removing NaN values, you can fill them with a specific value.
The
.fillna()
function
in Pandas allows you to do this.
filled_df = df.fillna(0)
print(filled_df)
Output:
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
1 0.0 45.0 3.6
2 4.2 0.0 0.0
3 3.8 60.0 4.5
In the code above,
fillna(0)
replaces all NaN values in the dataset with
0
.
Now, letâs create a heatmap with the NaN values filled:
plt.figure(figsize=(8, 4))
sns.heatmap(filled_df, annot=True, cmap='viridis')
plt.title('Heatmap (NaNs Filled with 0)')
plt.show()
Output:
Interpolate NaNs
Interpolation is another method to handle NaN values in datasets, especially when a linear relationship can be assumed between data points.
By interpolating, you estimate the NaN values based on neighboring data points.
This method is useful in time-series data or when the data points have a logical sequence.
interpolated_df = df.interpolate()
print(interpolated_df)
Output:
Call Quality Internet Speed Customer Satisfaction
0 3.50 50.0 4.00
1 3.85 45.0 3.60
2 4.20 52.5 4.05
3 3.80 60.0 4.50
Here,
interpolate()
function
calculates the NaN values by estimating them based on adjacent values. For example, in the âCall Qualityâ column, the NaN value is replaced with an average of its neighboring values.
Now, letâs visualize this interpolated data using a heatmap:
# Plotting the heatmap with interpolated values
plt.figure(figsize=(8, 4))
sns.heatmap(interpolated_df, annot=True, cmap='viridis')
plt.title('Heatmap (Interpolated NaN Values)')
plt.show()
Output:
Mask NaNs in the Heatmap
Masking NaNs in a heatmap allows you to visually distinguish these values from the rest of the data.
This method is useful when you want to maintain the original datasetâs structure, including the NaN values, while still providing a clear visual representation of where data is missing.
nan_mask = df.isna()
plt.figure(figsize=(8, 4))
sns.heatmap(df, annot=True, cmap='viridis', mask=nan_mask)
plt.title('Heatmap (NaNs Masked)')
plt.show()
Output:
The masked areas do not have annotations and color, clearly indicating the absence of data.
Use a Different Color for NaNs
Another method to handle NaNs in heatmaps is using a different color for these values.
This method highlights the NaNs distinctly and makes them easily identifiable.
First, weâll prepare a colormap that distinguishes NaNs:
from matplotlib.colors import ListedColormap
# Custom colormap: NaNs will be shown in grey
cmap = ListedColormap(sns.color_palette("viridis", as_cmap=True).colors + [(0.75, 0.75, 0.75)])
# Preparing the data: NaNs are set to a unique number
unique_number_for_nans = -1
heatmap_data = df.fillna(unique_number_for_nans)
plt.figure(figsize=(8, 4))
sns.heatmap(heatmap_data, annot=True, cmap=cmap, cbar=False)
plt.title('Heatmap (Different Color for NaNs)')
plt.show()
Output:
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology. |
| Markdown | [Skip to content](https://likegeeks.com/nan-values-seaborn-heatmap/#primary)
[](https://likegeeks.com/)
Menu
- [Home](https://likegeeks.com/ "Home")
- [Linux](https://likegeeks.com/linux/)
- [Linux Commands](https://likegeeks.com/linux/linux-commands/)
- [Bash Scripting](https://likegeeks.com/linux/bash-scripting/)
- [Server Administration](https://likegeeks.com/server-administration/)
- [Web Development](https://likegeeks.com/web-development/)
- [Python](https://likegeeks.com/python/)
- [NumPy](https://likegeeks.com/python/numpy/)
- [Pandas](https://likegeeks.com/python/pandas/)
- [Seaborn](https://likegeeks.com/python/seaborn/)
- [Tools](https://likegeeks.com/nan-values-seaborn-heatmap/)
- [Compare Lists](https://likegeeks.com/compare-two-lists/)
- [JSON To Table](https://likegeeks.com/json-to-table/)
- [JSON to Markdown](https://likegeeks.com/json-to-markdown/)
- [XML To Excel](https://likegeeks.com/xml-to-excel/)
[Home](https://likegeeks.com/) » [Python](https://likegeeks.com/python/) » [Seaborn](https://likegeeks.com/python/seaborn/)
# Handle NaN values in Seaborn heatmap
[Mokhtar Ebrahim](https://likegeeks.com/author/admin/)
Last Updated On:
[February 8, 2024](https://likegeeks.com/nan-values-seaborn-heatmap/)
One common challenge faced when creating heatmaps is the presence of NaN (Not a Number) values.
These NaNs can impact the visual output, leading to misleading interpretations or an unclear representation of the data.
In this tutorial, youâll learn how to handle NaN values in [Seaborn heatmaps](https://likegeeks.com/seaborn-heatmap-tutorial/).
Whether you are dealing with sparse NaNs that can be easily dropped, or require more nuanced approaches like filling, interpolating, or visually distinguishing NaNs, this tutorial covers it all.
**Table of Contents** [hide](https://likegeeks.com/nan-values-seaborn-heatmap/)
- [1 Remove NaN Values](https://likegeeks.com/nan-values-seaborn-heatmap/#Remove_NaN_Values)
- [2 Fill NaNs with a Specific Value](https://likegeeks.com/nan-values-seaborn-heatmap/#Fill_NaNs_with_a_Specific_Value)
- [3 Interpolate NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Interpolate_NaNs)
- [4 Mask NaNs in the Heatmap](https://likegeeks.com/nan-values-seaborn-heatmap/#Mask_NaNs_in_the_Heatmap)
- [5 Use a Different Color for NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Use_a_Different_Color_for_NaNs)
## Remove NaN Values
In scenarios where NaNs are sparse and not critical to your analysis, one effective method is to remove them using the `dropna()` method.
First, youâll need to import the necessary libraries and create a sample dataset.
```
import seaborn as sns
import pandas as pd
import numpy as np
data = {
'Call Quality': [3.5, np.nan, 4.2, 3.8],
'Internet Speed': [50, 45, np.nan, 60],
'Customer Satisfaction': [4.0, 3.6, np.nan, 4.5]
}
df = pd.DataFrame(data)
print(df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
1 NaN 45.0 3.6
2 4.2 NaN NaN
3 3.8 60.0 4.5
```
Note the NaN values in the dataset.
Next, letâs remove these NaN values:
```
cleaned_df = df.dropna()
print(cleaned_df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
3 3.8 60.0 4.5
```
In the above step, `dropna()` removes rows with any NaN values.
Letâs plot the heatmap using Seaborn after weâve dropped the NaN values.
This will help visualize how the removal of NaN values impacts the heatmap representation.
```
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
sns.heatmap(cleaned_df, annot=True, cmap='viridis')
plt.title('Heatmap (Without NaN Values)')
plt.show()
```
Output:

## Fill NaNs with a Specific Value
In certain situations, you want to retain the original size of your dataset, especially when the presence of each row is significant for your analysis.
In such cases, rather than removing NaN values, you can fill them with a specific value.
The [`.fillna()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.fillna.html) in Pandas allows you to do this.
```
filled_df = df.fillna(0)
print(filled_df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
1 0.0 45.0 3.6
2 4.2 0.0 0.0
3 3.8 60.0 4.5
```
In the code above, `fillna(0)` replaces all NaN values in the dataset with `0`.
Now, letâs create a heatmap with the NaN values filled:
```
plt.figure(figsize=(8, 4))
sns.heatmap(filled_df, annot=True, cmap='viridis')
plt.title('Heatmap (NaNs Filled with 0)')
plt.show()
```
Output:

## Interpolate NaNs
Interpolation is another method to handle NaN values in datasets, especially when a linear relationship can be assumed between data points.
By interpolating, you estimate the NaN values based on neighboring data points.
This method is useful in time-series data or when the data points have a logical sequence.
```
interpolated_df = df.interpolate()
print(interpolated_df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.50 50.0 4.00
1 3.85 45.0 3.60
2 4.20 52.5 4.05
3 3.80 60.0 4.50
```
Here, [`interpolate()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.interpolate.html) calculates the NaN values by estimating them based on adjacent values. For example, in the âCall Qualityâ column, the NaN value is replaced with an average of its neighboring values.
Now, letâs visualize this interpolated data using a heatmap:
```
# Plotting the heatmap with interpolated values
plt.figure(figsize=(8, 4))
sns.heatmap(interpolated_df, annot=True, cmap='viridis')
plt.title('Heatmap (Interpolated NaN Values)')
plt.show()
```
Output:

## Mask NaNs in the Heatmap
Masking NaNs in a heatmap allows you to visually distinguish these values from the rest of the data.
This method is useful when you want to maintain the original datasetâs structure, including the NaN values, while still providing a clear visual representation of where data is missing.
```
nan_mask = df.isna()
plt.figure(figsize=(8, 4))
sns.heatmap(df, annot=True, cmap='viridis', mask=nan_mask)
plt.title('Heatmap (NaNs Masked)')
plt.show()
```
Output:

The masked areas do not have annotations and color, clearly indicating the absence of data.
## Use a Different Color for NaNs
Another method to handle NaNs in heatmaps is using a different color for these values.
This method highlights the NaNs distinctly and makes them easily identifiable.
First, weâll prepare a colormap that distinguishes NaNs:
```
from matplotlib.colors import ListedColormap
# Custom colormap: NaNs will be shown in grey
cmap = ListedColormap(sns.color_palette("viridis", as_cmap=True).colors + [(0.75, 0.75, 0.75)])
# Preparing the data: NaNs are set to a unique number
unique_number_for_nans = -1
heatmap_data = df.fillna(unique_number_for_nans)
plt.figure(figsize=(8, 4))
sns.heatmap(heatmap_data, annot=True, cmap=cmap, cbar=False)
plt.title('Heatmap (Different Color for NaNs)')
plt.show()
```
Output:

- [Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https://likegeeks.com/nan-values-seaborn-heatmap/)
- [Tweet on Twitter](https://twitter.com/intent/tweet?text=Handle+NaN+values+in+Seaborn+heatmap&url=https://likegeeks.com/nan-values-seaborn-heatmap/&via=likegeeks)


[Mokhtar Ebrahim](https://likegeeks.com/author/admin/)
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.
## Related posts
1. [Seaborn heatmap tutorial (Python Data Visualization)](https://likegeeks.com/seaborn-heatmap-tutorial/ "Seaborn heatmap tutorial (Python Data Visualization)")
2. [Highlight Cells & Rows in Seaborn Heatmap](https://likegeeks.com/highlight-cells-rows-seaborn-heatmap/ "Highlight Cells & Rows in Seaborn Heatmap")
3. [Customize Seaborn Heatmap x-axis and y-axis Tick Labels](https://likegeeks.com/seaborn-heatmap-axis-tick-labels/ "Customize Seaborn Heatmap x-axis and y-axis Tick Labels")
4. [Apply Masks To Seaborn Heatmap in Python](https://likegeeks.com/seaborn-heatmap-masks/ "Apply Masks To Seaborn Heatmap in Python")
5. [Customize Colorbar in Seaborn Heatmap](https://likegeeks.com/seaborn-heatmap-colorbar/ "Customize Colorbar in Seaborn Heatmap")
6. [Customize Annotation Text in Seaborn Heatmap](https://likegeeks.com/annotation-text-seaborn-heatmap/ "Customize Annotation Text in Seaborn Heatmap")
###### Leave a Reply [Cancel reply](https://likegeeks.com/nan-values-seaborn-heatmap/#respond)
##### My Published Book
[](https://www.packtpub.com/en-us/product/mastering-linux-shell-scripting-9781788990554)
##### Related posts
1. [Seaborn heatmap tutorial (Python Data Visualization)](https://likegeeks.com/seaborn-heatmap-tutorial/)
2. [Highlight Cells & Rows in Seaborn Heatmap](https://likegeeks.com/highlight-cells-rows-seaborn-heatmap/)
3. [Customize Seaborn Heatmap x-axis and y-axis Tick Labels](https://likegeeks.com/seaborn-heatmap-axis-tick-labels/)
4. [Apply Masks To Seaborn Heatmap in Python](https://likegeeks.com/seaborn-heatmap-masks/)
5. [Customize Colorbar in Seaborn Heatmap](https://likegeeks.com/seaborn-heatmap-colorbar/)
6. [Customize Annotation Text in Seaborn Heatmap](https://likegeeks.com/annotation-text-seaborn-heatmap/)
## Post navigation
[Highlight Cells & Rows in Seaborn Heatmap](https://likegeeks.com/highlight-cells-rows-seaborn-heatmap/)
[Customize Seaborn Heatmap x-axis and y-axis Tick Labels](https://likegeeks.com/seaborn-heatmap-axis-tick-labels/)
- [Disclaimer](https://likegeeks.com/disclaimer/)
- [Privacy Policy](https://likegeeks.com/privacy-policy/)
- [About](https://likegeeks.com/about/)
- [Contact](https://likegeeks.com/contact/) |
| Readable Markdown | One common challenge faced when creating heatmaps is the presence of NaN (Not a Number) values.
These NaNs can impact the visual output, leading to misleading interpretations or an unclear representation of the data.
In this tutorial, youâll learn how to handle NaN values in [Seaborn heatmaps](https://likegeeks.com/seaborn-heatmap-tutorial/).
Whether you are dealing with sparse NaNs that can be easily dropped, or require more nuanced approaches like filling, interpolating, or visually distinguishing NaNs, this tutorial covers it all.
- [1 Remove NaN Values](https://likegeeks.com/nan-values-seaborn-heatmap/#Remove_NaN_Values)
- [2 Fill NaNs with a Specific Value](https://likegeeks.com/nan-values-seaborn-heatmap/#Fill_NaNs_with_a_Specific_Value)
- [3 Interpolate NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Interpolate_NaNs)
- [4 Mask NaNs in the Heatmap](https://likegeeks.com/nan-values-seaborn-heatmap/#Mask_NaNs_in_the_Heatmap)
- [5 Use a Different Color for NaNs](https://likegeeks.com/nan-values-seaborn-heatmap/#Use_a_Different_Color_for_NaNs)
## Remove NaN Values
In scenarios where NaNs are sparse and not critical to your analysis, one effective method is to remove them using the `dropna()` method.
First, youâll need to import the necessary libraries and create a sample dataset.
```
import seaborn as sns
import pandas as pd
import numpy as np
data = {
'Call Quality': [3.5, np.nan, 4.2, 3.8],
'Internet Speed': [50, 45, np.nan, 60],
'Customer Satisfaction': [4.0, 3.6, np.nan, 4.5]
}
df = pd.DataFrame(data)
print(df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
1 NaN 45.0 3.6
2 4.2 NaN NaN
3 3.8 60.0 4.5
```
Note the NaN values in the dataset.
Next, letâs remove these NaN values:
```
cleaned_df = df.dropna()
print(cleaned_df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
3 3.8 60.0 4.5
```
In the above step, `dropna()` removes rows with any NaN values.
Letâs plot the heatmap using Seaborn after weâve dropped the NaN values.
This will help visualize how the removal of NaN values impacts the heatmap representation.
```
import matplotlib.pyplot as plt
plt.figure(figsize=(8, 4))
sns.heatmap(cleaned_df, annot=True, cmap='viridis')
plt.title('Heatmap (Without NaN Values)')
plt.show()
```
Output:

## Fill NaNs with a Specific Value
In certain situations, you want to retain the original size of your dataset, especially when the presence of each row is significant for your analysis.
In such cases, rather than removing NaN values, you can fill them with a specific value.
The [`.fillna()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.fillna.html) in Pandas allows you to do this.
```
filled_df = df.fillna(0)
print(filled_df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.5 50.0 4.0
1 0.0 45.0 3.6
2 4.2 0.0 0.0
3 3.8 60.0 4.5
```
In the code above, `fillna(0)` replaces all NaN values in the dataset with `0`.
Now, letâs create a heatmap with the NaN values filled:
```
plt.figure(figsize=(8, 4))
sns.heatmap(filled_df, annot=True, cmap='viridis')
plt.title('Heatmap (NaNs Filled with 0)')
plt.show()
```
Output:

## Interpolate NaNs
Interpolation is another method to handle NaN values in datasets, especially when a linear relationship can be assumed between data points.
By interpolating, you estimate the NaN values based on neighboring data points.
This method is useful in time-series data or when the data points have a logical sequence.
```
interpolated_df = df.interpolate()
print(interpolated_df)
```
Output:
```
Call Quality Internet Speed Customer Satisfaction
0 3.50 50.0 4.00
1 3.85 45.0 3.60
2 4.20 52.5 4.05
3 3.80 60.0 4.50
```
Here, [`interpolate()` function](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.interpolate.html) calculates the NaN values by estimating them based on adjacent values. For example, in the âCall Qualityâ column, the NaN value is replaced with an average of its neighboring values.
Now, letâs visualize this interpolated data using a heatmap:
```
# Plotting the heatmap with interpolated values
plt.figure(figsize=(8, 4))
sns.heatmap(interpolated_df, annot=True, cmap='viridis')
plt.title('Heatmap (Interpolated NaN Values)')
plt.show()
```
Output:

## Mask NaNs in the Heatmap
Masking NaNs in a heatmap allows you to visually distinguish these values from the rest of the data.
This method is useful when you want to maintain the original datasetâs structure, including the NaN values, while still providing a clear visual representation of where data is missing.
```
nan_mask = df.isna()
plt.figure(figsize=(8, 4))
sns.heatmap(df, annot=True, cmap='viridis', mask=nan_mask)
plt.title('Heatmap (NaNs Masked)')
plt.show()
```
Output:

The masked areas do not have annotations and color, clearly indicating the absence of data.
## Use a Different Color for NaNs
Another method to handle NaNs in heatmaps is using a different color for these values.
This method highlights the NaNs distinctly and makes them easily identifiable.
First, weâll prepare a colormap that distinguishes NaNs:
```
from matplotlib.colors import ListedColormap
# Custom colormap: NaNs will be shown in grey
cmap = ListedColormap(sns.color_palette("viridis", as_cmap=True).colors + [(0.75, 0.75, 0.75)])
# Preparing the data: NaNs are set to a unique number
unique_number_for_nans = -1
heatmap_data = df.fillna(unique_number_for_nans)
plt.figure(figsize=(8, 4))
sns.heatmap(heatmap_data, annot=True, cmap=cmap, cbar=False)
plt.title('Heatmap (Different Color for NaNs)')
plt.show()
```
Output:


Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology. |
| Shard | 88 (laksa) |
| Root Hash | 15338997981204926088 |
| Unparsed URL | com,likegeeks!/nan-values-seaborn-heatmap/ s443 |