By Prajwal CN
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
In the previous articles, we have seen how to perform EDA using graphical methods. In this article, we will be focusing on Python functions used for Exploratory Data Analysis in Python. As we all know, how important EDA is it provides a brief understanding of the data. So, without wasting much time, let’s roll!
Well, first things first. We will load the titanic dataset into python to perform EDA.
#Load the required libraries import pandas as pd import numpy as np import seaborn as sns #Load the data df = pd.read_csv('titanic.csv') #View the data df.head()
Our data is ready to be explored!
The df.info() function will give us the basic information about the dataset. For any data, it is good to start by knowing its information. Let’s see how it works with our data.
#Basic information df.info() #Describe the data df.describe()
Using this function, you can see the number of null values, datatypes, and memory usage as shown in the above outputs along with descriptive statistics.
You can use the df.duplicate.sum() function to the sum of duplicate value present if any. It will show the number of duplicate values if they are present in the data.
#Find the duplicates df.duplicated().sum()
Well, the function returned ‘0’. This means, there is not a single duplicate value present in our dataset and it is a very good thing to know.
You can find the number of unique values in the particular column using
unique() function in python.
#unique values df['Pclass'].unique() df['Survived'].unique() df['Sex'].unique()
array([3, 1, 2], dtype=int64) array([0, 1], dtype=int64) array(['male', 'female'], dtype=object)
The unique() function has returned the unique values which are present in the data and it is pretty much cool!
Yes, you can visualize the unique values present in the data. For this, we will be using the seaborn library. You have to call the sns.countlot() function and specify the variable to plot the count plot.
#Plot the unique values sns.countplot(df['Pclass']).unique()
That’s great! You are doing good. It is as simple as that. Though EDA has two approaches, a blend of graphical and non-graphical will give you the bigger picture altogether.
Finding the null values is the most important step in the EDA. As I told many a time, ensuring the quality of data is paramount. So, let’s see how we can find the null values.
#Find null values df.isnull().sum()
PassengerId 0 Survived 0 Pclass 0 Name 0 Sex 0 Age 177 SibSp 0 Parch 0 Ticket 0 Fare 0 Cabin 687 Embarked 2 dtype: int64
Oh no, we have some null values in the ‘Age’ and ‘Cabin’ variables. But, don’t worry. We will find a way to deal with them soon.
Hey, we got a
replace() function to replace all the null values with a specific data. It is too good!
#Replace null values df.replace(np.nan,'0',inplace = True) #Check the changes now df.isnull().sum()
PassengerId 0 Survived 0 Pclass 0 Name 0 Sex 0 Age 0 SibSp 0 Parch 0 Ticket 0 Fare 0 Cabin 0 Embarked 0 dtype: int64
Whoo! That’s awesome. It is very easy to find and replace the null values in the data as shown. I have used 0 to replace null values. You can even opt for more meaningful methods such as mean or median.
Knowing the datatypes which you are exploring is very important and an easy process too. Let’s see how it works.
PassengerId int64 Survived int64 Pclass int64 Name object Sex object Age object SibSp int64 Parch int64 Ticket object Fare float64 Cabin object Embarked object dtype: object
That’s it. You have to use the dtypes function for this a shown and you will get the datatypes of each attribute.
Yes, you can filter the data based on some logic.
#Filter data df[df['Pclass']==1].head()
You can see that the above code has returned only data values that belong to class 1.
You can create a box plot for any numerical column using a single line of code.
Finally, to find the correlation among the variables, we can make use of the correlation function. This will give you a fair idea of the correlation strength between different variables.
This is the correlation matrix with the range from +1 to -1 where +1 is highly and positively correlated and -1 will be highly negatively correlated.
You can even visualize the correlation matrix using seaborn library as shown below.
#Correlation plot sns.heatmap(df.corr())
EDA is the most important part of any analysis. You will get to know many things about your data. You will find answers to your most of the questions with EDA. I have tried to show most of the python functions used for exploring the data with visualizations. I hope you got something from this article.
That’s all for now! Happy Python :)
More read: Exploratory Data analysis
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.Sign up now