Python Tutorial: Handling missing values
Skills:
Data Literacy70%
Key Takeaways
This video tutorial covers handling missing values in Python using the pandas library, specifically using the read_csv function with the na_values argument and detecting hidden missing values using domain knowledge.
Full Transcript
in the previous lesson you were introduced to the two null value types that you encountered in Python in this lesson you will assign null values to the missing values in the data set missing values in a data set aren't usually left unfilled they are filled with dummy values like n/a - or period etc in this lesson you will learn to detect such missing values as well as replace them with nan let's use the college data set which contains various details of college students as an example we'll load the data using PT dot read CSV of college dot CSV the first step in analyzing the data set is to read and print a snippet of the data set will print the head of the College data frame find that all columns have float values if you observe clearly you can see that a few data points are filled with a period this suggests that missing values might be represented by a period however we can confirm this only through further analysis we'll use the info method to get a gist of the data set hey something's odd here all the columns except private or of object type although they are supposed to be float we can further explore and confirm by finding the unique values in one of the columns this way we can find any non numerical values let's apply the unique method on the column C SAT and sort them using MP dot sort from the output you can clearly observe that period is the only string value present hence we need to replace it with nan this can be simply achieved while loading the data set to a data frame you can use the argument na underscore values and be read or read CSV to specify the values for missing data if you again check the info of college you'll find that all the columns are now float64 type this is great now let's consider another data set to detect hidden missing values we will use the Pima Indians tab it is data set which contains various clinical diagnostic information of the patients from the Pima community while loading the data set we can observe non values for missing data when you bring the head of the data frame as before let's print the info of the college data frame they are all float or in type as expected further we can analyze using the described method on the damages data frame observe closely something very odd here is that the PMI column has a minimum value of 0 but we are aware that BMI cannot be 0 hence the zeros must rather be missing values in disguise to confirm the same we can filter all the rows where BMI is 0 there are 11 rows which have BMI as 0 they must be missing values these types of missing values can be tricky as they require some level of domain knowledge will replace these 11 rows of PMI column with nan and check again using NP dot is NaN of tab it is dot PMI great now that we have successfully removed the hidden missing values and replace them with Nance let's summarize what we learned in this lesson we learn to detect missing value characters like period detect the inherent missing values within the data like 0 and replace them with Nan's in the next lesson you'll dig deeper into analyzing the missed
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/dealing-with-missing-data-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
In the previous lesson you were introduced to the two null value types that you encounter in python. In this lesson, you will assign null values to the missing values in the dataset!
Missing values in a dataset aren't usually left unfilled, they are filled with dummy values like 'NA', '-' or '.' etc.
In this lesson, you will learn to detect such missing values as well as replace them with 'NaN'.
Let's use the 'college' dataset which contains various details of college students as an example. We'll load data using 'pd.read_csv()' of 'college.csv'.
The first step in analyzing the dataset is to read and print a snippet of the dataset. We'll print the head of the 'college' DataFrame.
Find that all columns have float values.
If you observe clearly, you can see that a few data points are filled with a period! This suggests that missing values might be represented by a period.
However, we can confirm this only through further analysis. We'll use the info() method to get a gist of the dataset.
Hey, somethings' odd here! All the columns except 'private' are of 'object' type although they are supposed to be float.
We can further explore and confirm by finding the unique values in one of the columns. This way we can find any non-numerical values!
Let's apply the '.unique()' method on the column 'csat' and sort them using 'np.sort()'. From the output you can clearly observe that '.' is the only string value present. Hence, we need to replace it with 'NaN'.
This can be simply achieved while loading the dataset to a DataFrame. You can use the argument 'na_values' in 'pd.read_csv' to specify the values for missing data.
If you again check the 'info()' of 'college', you'll find that all the columns are now 'float64' type. This is great!
Now
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Data Literacy
View skill →
🎓
Tutor Explanation
DeepCamp AI