Python Tutorial: Handling missing values

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video teaches how to handle missing values in Python using pandas, including detecting and replacing missing values with NaN. It covers the use of the read_csv function with the na_values argument and detecting hidden missing values using domain knowledge.

Key Takeaways
  1. Load the data using pandas read_csv function
  2. Print a snippet of the data set using the head method
  3. Use the info method to get a gist of the data set
  4. Find unique values in a column using the unique method
  5. Replace missing values with NaN using the na_values argument
  6. Detect hidden missing values using domain knowledge
💡 Hidden missing values can be tricky to detect and require domain knowledge to identify.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →