Python Tutorial: Handling errors and missing data
Skills:
Data Literacy70%
Key Takeaways
This video tutorial demonstrates how to handle errors and missing data when importing flat files using Python's pandas library, specifically using the read_csv function with parameters such as dtype, na_values, error_bad_lines, and warn_bad_lines.
Full Transcript
so far we've imported flat files with minor tweaks to set column names and manage the amount of data loaded this is enough if the data is already in great shape but what if there are issues with the data or the import common issues include incorrect column data types which can hinder analysis missing values indicated with custom designators or records that pandas cannot read luckily read CSV offers ways to address these issues during import reducing the wrangling needed later on when importing data pandas infers each columns data type sometimes a guess is wrong checking the data types in the tax data we see that pandas interpreted zip codes as integers they're more accurately modeled as strings though zip codes are not quantities and include meaningful leading zeros instead of letting pandas guess we can set the data type of any or all columns with read CSV SD type keyword argument D type takes a dictionary where each key is a column name and each value is the data type that column should be note that non-standard data types like pandas categories must be passed in quotations here we specify that the zip code column should contain strings leaving pandas to infer the other columns printing the new data frames D types we see that zip codes D type is object which is the pandas counterpart to Python strings missing data is another common issue pandas automatically recognizes some values like n/a or null as missing data enabling the use of handy data cleaning functions but sometimes missing values are represented in ways that pandas won't catch such as with dummy codes in the tax data records were sorted so that the first few have the zip code zero which is not a valid code and should be treated as missing we can tell pandas to consider these missing data with the n/a values keyword argument na values accepts either a single value a list of values or a dictionary of columns and values in that column to treat as missing let's pass a dictionary specifying that any zeros in zip code should be treated as missing data then we filter the data using the is n/a method on the zip code column to view rows with missing postal codes one last issue you may face are lines that pandas just can't parse for example a record could have more values than there are columns like the second record in this corrupted version of the tax data let's try reading it by default trying to load this file results in the long error and no data is imported luckily we can change this behavior with two arguments error bad lines and warned bad lines both take boolean or true false values setting error bad lines to false makes pandas skip bad lines and load the rest of the data instead of throwing an error warned bad lines tells pandas whether to display messages when unpossible lines are skipped let's try importing the corrupted file again this time with error bad lines set to false and warn bad lines equal to true success a word of caution if lines were skipped it's worth investigating what was left out to see if there are underlying issues that should be addressed in this video you've learned about common issues encountered importing data from flat files and how to handle those issues now it's your turn to practice
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/streamlined-data-ingestion-with-pandas at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
So far, we've imported flat files with minor tweaks to set column names and manage the amount of data loaded. This is enough if the data is already in great shape. But what if there are issues with the data or the import?
Common issues include incorrect column data types, which can hinder analysis, missing values indicated with custom designators, or records that pandas cannot read. Luckily, read CSV offers ways to address these issues during import, reducing the wrangling needed later on.
When importing data, pandas infers each column's data type. Sometimes, it guesses wrong.
Checking data types in the tax data, we see that pandas interpreted ZIP codes as integers. They're more accurately modeled as strings, though -- ZIP codes are not quantities and include meaningful leading zeros.
Instead of letting pandas guess, we can set the data type of any or all columns with read CSV's dtype keyword argument.
Dtype takes a dictionary, where each key is a column name and each value is the data type that column should be. Note that non-standard data types, like pandas categories, must be passed in quotations.
Here, we specify that the zipcode column should contain strings, leaving pandas to infer the other columns.
Printing the new data frame's dtypes, we see that zipcode's dtype is "object", which is the pandas counterpart to Python strings.
Missing data is another common issue. pandas automatically recognizes some values, like “N/A” or “null”, as missing data, enabling the use of handy data-cleaning functions. But sometimes missing values are represented in ways that pandas won't catch, such as with dummy codes.
In the tax data, records were sorted so that the first few have the ZIP code 0, which is not a valid code and should be
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