> For the complete documentation index, see [llms.txt](https://laboratory-of-lipid-metabolism-a.gitbook.io/omics-data-visualization-in-r-and-python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://laboratory-of-lipid-metabolism-a.gitbook.io/omics-data-visualization-in-r-and-python/missing-values-handling-in-python/detecting-missing-values.md).

# Detecting missing values

## Required packages

The required packages for this section are pandas, matplotlib and seaborn. These can be installed with the following command in the command window (Windows) / terminal (Mac).

```
pip install pandas matplotlib seaborn
```

## Loading the data

{% file src="/files/1f5syZV6urkUaZVGexSc" %}

Place the downloaded Lipidomics\_missing\_values\_EXAMPLE.xlsx file in the same folder as your JupyterLab script. Then run the following code in Jupyter:

```python
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

df = pd.read_excel("Matrix_missing_values_EXAMPLE.xlsx", decimal=",")
df.set_index("Sample Name", inplace=True)
```

We can generate a heatmap visualisation of the missing values across the table (white values indicate a missing value):

```python
plt.figure(figsize=(24, 30))  # Modify the width and height as needed
sns.heatmap(df.isnull(), cbar=False)
plt.savefig("missing_values_heatmap.png", dpi=200, bbox_inches='tight') 
plt.show()
```

<figure><img src="https://1939159422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG1KwOmfbAgmKed8MHinY%2Fuploads%2FDvsD1t7INJkXJfwrzN8F%2Fmissing_values_heatmap_python.png?alt=media&amp;token=478e214f-9b65-44b8-b698-ab9480653903" alt=""><figcaption><p>Heatmap indicating missing values in the data table, white values indicate missing data.</p></figcaption></figure>

We can visualize the % missing values in the samples:

```python
# Calculate percentage of missing values per row (sample)
missing_percentage_per_sample = df.isnull().mean(axis=1) * 100

# Bar plot for missing values per sample
plt.figure(figsize=(30, 6))
missing_percentage_per_sample.sort_values(ascending=False).plot(kind='bar')
plt.title("Percentage of Missing Values per sample")
plt.xlabel("Samples")
plt.ylabel("Percentage Missing")
plt.tight_layout()
plt.show()
```

<figure><img src="https://1939159422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG1KwOmfbAgmKed8MHinY%2Fuploads%2FnxxY5KQkLynU1IF5Cs8l%2Fmissing_values_species_barplot_python.png?alt=media&amp;token=86cd8b8e-c1f3-41eb-b8ed-c00d00b257b1" alt=""><figcaption></figcaption></figure>

And for the species:

```python
# Calculate percentage of missing values per row (sample)
missing_percentage_per_sample = df.isnull().mean(axis=1) * 100

# Bar plot for missing values per sample
plt.figure(figsize=(30, 6))
missing_percentage_per_sample.sort_values(ascending=False).plot(kind='bar')
plt.title("Percentage of Missing Values per sample")
plt.xlabel("Samples")
plt.ylabel("Percentage Missing")
plt.tight_layout()
plt.show()
```

<figure><img src="https://1939159422-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FG1KwOmfbAgmKed8MHinY%2Fuploads%2Fzk69CfkIMFof49sQUkee%2Fmissing_values_samples_barplot_python.png?alt=media&amp;token=ea226bcd-98da-4c0e-9307-b44bfbef3960" alt=""><figcaption></figcaption></figure>
