How to Perform Exploratory Data Analysis with Python
Tags: python, datascience, womenintech, analytics
Published: 2023-02-19 06:56:37

#Introduction

Exploratory data analysis (EDA) is an essential step in the data science process which involves use of both statistical methods and data visualization techniques to uncover patterns, trends, understand relationships and gain meaningful insights from data in order to understand the problem and make informed decisions. Statistics show that the amount of data generated worldwide is growing at an exponential rate. It is estimated that by 2025, the world will generate 463 exabytes of data per day, thus data scientists and data analysts should understand the process.

Whether you’re an experienced data scientist or a beginner, this blog will walk you through the exciting process of EDA and how to use Python to perform this essential task. 

So buckle up, it’s time to dive into the world of data and unearth some hidden insights.

> “Data is like garbage. You’d better know what you are going to do with it before you collect it.” — Mark Twain.

###Why Exploratory data analysis (EDA)) is an essential task.

- EDA helps data practitioners understand and gain insights from data before applying machine learning and statistical techniques.
- EDA helps identify patterns, anomalies, and relationships within the data so as to make informed decisions and develop effective strategies.
- The EDA process aims in detecting faulty points in data such as errors or missing values which can be corrected by analysis.

####Exploratory data analysis (EDA) steps.

To achieve this critical task, following steps need to be taken into consideration.
- Importing necessary libraries.
- Load the dataset
- Viewing the dataset.
- Check for duplication.
- Data preparation (Handling missing values and outliers)
- Analyzing the data.(univariate, bivariate and multivariate analysis)
- Visualization.

Wondering where to get datasets for practice? We got you covered. Take a look at the following resources.

- [Google Dataset Search](https://datasetsearch.research.google.com/)
- [Kaggle](https://www.kaggle.com/datasets)
- [Data.Gov](https://data.gov/)
- [Datahub.io](https://datahub.io/search)
- [UCI Machine Learning Repository](https://archive.ics.uci.edu/ml/datasets.php)
- [Earth Data](https://www.earthdata.nasa.gov/)

Lets now perform EDA on the following sample Dataset  from Kaggle by performing the following steps;

[Dataset](https://raw.githubusercontent.com/PhylisKorir/SalesPrediction-using-Linear-Regression/main/Electronic_sales.csv)

_**Importing necessary Libraries**_

Python is a versatile programming language with robust libraries for data analysis, including Pandas, NumPy, seaborn, Matplotlib, Plotly, Tensor flow, Keras. 
We will make use of the following modules

```Python
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
```
Get to understand other Python libraries for data analysis by reading through the following article.

[Data Science Libraries Every Data scientist should know.](https://dev.to/phylis/python-libraries-every-data-scientist-must-know-3ih6)

_**Loading the Dataset.**_

The pandas library can be used to read various datasets from multiple formats such as csv, txt, Excel, JSON, SQL, etc.

```Python
#How to read Datasets of various formats
import pandas as pd
#read csv
df = pd.read_csv('filename.csv')
#read txt
df = pd.read_csv('filename.txt', sep="\t")
#read excel
df = pd.read_excel('filename.xlsx')
#read json
df = pd.read_json('filename.json')
#read sql
df = pd.read_sql('SELECT * FROM TableName', connection)
```
Our dataset is in csv format, we load it into data DataFrame as follows;

```Python
data=pd.read_csv('https://raw.githubusercontent.com/PhylisKorir/SalesPrediction-using-Linear-Regression/main/Electronic_sales.csv')
data
```
_**Viewing the DataFrame.**_

We can quickly find out how many rows and columns there are in our dataset by using the shape method. This returns a tuple which contains the number of rows and columns.

_**Shape of the Data**_

```Python
data.shape
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7g8pjlax0fter7xu1vrp.png)

_**Previewing the Dataset.**_

Preview the first 5 rows.

```Python
data.head()
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ufs6u73t93yw907ziwg.png)

Preview last five rows.

```Python
data.tail()
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5l2rzo29ovvskqnytn2.png)

head() and tail() functions are used to preview the first five rows and last fie rows in the dataset respectively.

_**Columns names**_

```Python
data.columns
```
The pandas.DataFrame.columns function is used to get the names of all the columns of a pandas dataframe object. It returns an Index object which holds the column labels.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ycycb6gt7a2bjftkp15g.png)

_**Concise info of dataset**_

```Python
data.info()
```
The info() method allows us to obtain additional information about the dataset, such as the names of the columns, the data type of each column, and the number of non-null values.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iv5zxwlat728cykuwzvo.png)

_**Data types**_

```Python
data.dtypes
```

The pandas.dtypes function returns the data types of each column in a pandas DataFrame. It returns a pandas Series with the data type of each column.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tm4vk5n2od8r8mzrc8w.png)

_**Descriptive statistics.**_

```Python
data.describe()
```
The describe() function in pandas is used to generate descriptive statistics that summarize the central tendency, dispersion and shape of a dataset's distribution, excluding NaN values. It provides the count, mean, standard deviation, minimum, maximum, 25th percentile, 50th percentile (median) and 75th percentile of the data.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9si44s036ey6dk7cy2aj.png)

_**Checking Duplicates**_

```Python
data.duplicated.sum()
```
The pandas.DataFrame.duplicated.sum() function returns a Series or DataFrame containing the sum of the boolean values (True or False) in the duplicate rows of the DataFrame.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/679df0qwe8x6xzql2999.png)
The dataset has no duplicates.

Unique Values


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33y34m1oglkmvzx9pcbh.png)

_**DATA CLEANING**_
Data cleaning is the process of fixing or removing incorrect, corrupted, incorrectly formatted, duplicate, or incomplete data within a dataset. It involves handling missing values and handling outliers

_**Missing values**_

We will begin by scanning the dataset for missing values. We may do this by utilizing the isna() method, which returns a dataframe of boolean values indicating whether or not a field is null. We can use the sum() method to group all missing values by column.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/823xp5848jqkoecicpbh.png)

Our dataset has no null values and its ready for analysis.

It is common for datasets to contain errors, missing values, outliers, or other types of inconsistencies. Incase your dataset has missing values this blog [(Handling Missing Values)](https://dev.to/phylis/how-to-handle-missing-values-in-a-dataset-using-python-1d2e)will guide you how to handle missing values in data.

> "No data is clean, but most is useful."~ Dean Abbott, Co-founder and Chief Data Scientist at SmarterHQ

_**Analyzing Data**_

With our cleansed dataset we can go ahead and begin the task of exploring the data. 

<u>**Univariate Analysis.**</u>
Univariate analysis is a form of exploratory data analysis (EDA) that involves the examination of a single variable. It is used to summarize the data and gain insight into the data's distribution, central tendency, and variability. It can be used to answer questions such as what is the range of the data, what is the most common value, and is there any outliers. It is also used to identify any trends or patterns in the data.

we can visualize this information using a boxplot from Seaborn.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju6pd2kurjq4oonw2we5.png)

```Python
#Handling outliers
fig, axs = plt.subplots(3, figsize = (5,5))
plt1 = sns.boxplot(data['TV'], ax = axs[0])
plt2 = sns.boxplot(data['Newspaper'], ax = axs[1])
plt3 = sns.boxplot(data['Radio'], ax = axs[2])
plt.tight_layout()
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zj6jh9lg1s2whjq11lqr.png)

With the above box plots we can be able distribution of the data in the vriables.


<u>**Bivariate analysis**</u>

Bivariate analysis involves analyzing data with two variables or columns. This is usually a way to explore the relationships between these variables and how they influence each other, if at all. 

```Python
# Let's see how Sales are related with other variables using scatter plot.
sns.pairplot(data, x_vars=['TV', 'Newspaper', 'Radio'], y_vars='Sales', height=4, aspect=1, kind='scatter')
plt.show()
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kst7fpm9bxpnsq22n7ef.png)

<u>**Multivariate analysis**</u>

Multivariate analysis is a type of data analysis that involves examining more than two variables at once in order to better understand the relationships between them. It is a powerful tool for exploratory data analysis, as it allows researchers to identify patterns and trends in large datasets that would otherwise be difficult to spot. It also allows researchers to explore the relationships between multiple variables and determine which ones are most important in predicting the outcome of interest. 

```Python
# Let's see the correlation between different variables.
sns.heatmap(data.corr(), cmap="Greens", annot = True)
plt.show()
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hoeelgxa1odp5bnnxd1.png)

**_As it is visible from the scatterplot and the heatmap, the variable TV seems to be most correlated with Sales_**

**Conclusions**

I hope this article has been informative and helpful in understanding how to perform Exploratory Data Analysis with Python. If you found it helpful, please share it with your fellow colleagues and friends. Enjoy your exploration of data!