Completeness
1. Completeness
Hi and welcome to the last lesson of this chapter. In this lesson, we're going to discuss completeness and missing data.2. What is missing data?
Missing data is one of the most common and most important data cleaning problems. Essentially, missing data is when no data value is stored for a variable in an observation. Missing data is most commonly represented as NA or NaN, but can take on arbitrary values like 0 or dot. Like a lot of the problems that we've seen thus far in the course, it's commonly due to technical or human errors. Missing data can take many forms, so let's take a look at an example.3. Airquality example
Let's take a look at the airquality dataset. It contains temperature and CO2 measurements for different dates.4. Airquality example
We can see that the CO2 value in this row is represented as NaN5. Airquality example
We can find rows with missing values by using the dot is na method, which returns True for missing values and False for complete values across all our rows and columns.6. Airquality example
We can also chain the isna method with the sum method, which returns a breakdown of missing values per column in our dataframe. We notice that the CO2 column is the only column with missing values - let's find out why and dig further into the nature of this missingness by first visualizing our missing values.7. Missingno
The missingno package allows to create useful visualizations of our missing data. Digging into its details is not part of the course, but you can also check out other courses on missing data in DataCamp's course library. We visualize the missingness of the airquality DataFrame with the msno dot matrix function, and show it with pyplot's show function from matplotlib, which returns8. Insert title here...
the following image. This matrix essentially shows how missing values are distributed across a column. We see that missing CO2 values are randomly scattered throughout the column, but is that really the case? Let's dig deeper.9. Airquality example
We first isolate the rows of airquality with missing CO2 values in one DataFrame, and complete CO2 values in another.10. Airquality example
Then, let's use the describe method on each of the created DataFrames.11. Airquality example
We see that for all missing values of CO2, they occur at really low temperatures, with the mean temperature at minus 39 degrees and a minimum and maximum of -49 and -30 respectively. Let's confirm this visually with the missngno package.12. Insert title here...
We first sort the DataFrame by the temperature column. Then we input the sorted dataframe to the matrix function from msno. This leaves us with this matrix.13. Insert title here...
Notice how all missing values are on the top? This is because values are sorted from smallest to largest by default. This essentially confirms that CO2 measurements are lost for really low temperatures. Must be a sensor failure!14. Missingness types
This leads us to missingness types. Without going too much into the details, there are a variety of types of missing data. It could missing completely at random, missing at random, or missing not at random.15. Missingness types
Missing completely at random data is when there missing data completely due to randomness, and there is no relationship between missing data and remaining values, such data entry errors.16. Missingness types
Despite a slightly deceiving name, Missing at random data is when there is a relationship between missing data and other observed values, such as our CO2 data being missing for low temperatures.17. Missingness types
When data is missing not at random, there is a systematic relationship between the missing data and unobserved values. For example, when it's really hot outside, the thermometer might stop working, so we don't have temperature measurements for days with high temperatures. However, we have no way to tell this just from looking at the data since we can't actually see what the missing temperatures are.18. How to deal with missing data?
There's a variety of ways of dealing with missing data, from dropping missing data, to imputing them with statistical measures such as mean, median or mode, or imputing them with more complicated algorithmic approaches or ones that require some machine learning. Each missingness type requires a specific approach, and each type of approach has drawbacks and positives, so make sure to dig deeper in DataCamp's course library on dealing with missing data.19. Dealing with missing data
In this lesson, we'll just explore the simple approaches to dealing with missing data. Let's grab another look at the header of airquality.20. Dropping missing values
We can drop missing values, by using the dot dropna method, alongside the subset argument which lets us pick which column's missing values to drop.21. Replacing with statistical measures
We can also replace the missing values of CO2 with the mean value of CO2, by using the fillna method, which is in this case 1.73. Fillna takes in a dictionary with columns as keys, and the imputed value as values. We can even feed custom values into fillna pertaining to our missing data if we have enough domain knowledge about our dataset.22. Let's practice!
Now that you know how to tackle missing data, let's get started!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.