{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Identifying Data Problems" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In a perfect world, our datasets would all arrive containing only accurate data formatted in exactly the way we need. Sadly, the world we live in is *not* perfect. Either because of clumsy data entry, bad translation between data formats, survey participants trying to be jerks, or the coding errors of other data scientists, messy data is a fact of life. As a result, a key part of being a data scientist is to learn to (a) seek out and identify data cleanliness problems, and (b) learn to correct them. \n", "\n", "These parts of data science are not nearly as cool or widely discussed as new machine learning packages or the latest learning algorithm, but as anyone who has done data science in the real world will tell you, data cleaning and wrangling will take up a very large portion of your working life. Indeed, it is often said that \"data scientists spend 80% of their time cleaning data and only 20% doing analyses\" (or, in the version I prefer, \"data scientists spend 80% of their time cleaning data and the other 20% of their time complaining about cleaning data.\") That's probably a little high on average—a [recent survey by Anaconda](https://www.anaconda.com/state-of-data-science-2020) found on average data scientists reported data wrangling took up about 45% of their time—but what is undisputed is its importance to being a successful data scientist.\n", "\n", "So in this reading we will discuss tools in pandas for identifying problems before we turn to tools for correcting problems in our next reading!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Identifying Data Problems in Pandas \n", "\n", "Everything I said above may sound... well, awful, but here's the good news: once you get into it, data cleaning begins to feel less like grunt work and more like being a detective, and it can actually be a lot of fun.\n", "\n", "\n", "### Your Challenge\n", "\n", "Suppose you have been asked to calculate the overall US employment rate and average incomes for men and women, as well as the employment rate and average income of people in their 30s and 40s. To accomplish this task, you have been handed a 1% sample of the US American Community Survey—a representative survey of residents of the United States published by the US Census Bureau. This is real data, and so like all real data comes with lots of quirk and oddities we'll need to navigate.\n", "\n", "Moreover, you've even been told that the variable for gender in the dataset is `sex`, the variable for age is `age`, the variable for income is `inctot`, and the variable for whether someone is employed is `empstat`. Should be pretty easy, right? Let's do it!\n", "\n", "So the first thing, of course, is we'll load the data and subset for the variables we want:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "# Here we load the American Community Survey data.\n", "# Note that here we're using a data loading trick\n", "# we didn't discuss in detail in our previous readings:\n", "# pandas will read data directly from a URL!\n", "\n", "acs = pd.read_stata(\n", " \"https://github.com/nickeubank/MIDS_Data/blob/master\"\n", " \"/US_AmericanCommunitySurvey/US_ACS_2017_10pct_sample.dta?raw=true\"\n", ")\n", "\n", "# For this exercise, we'll focus on just these five variables,\n", "# which from the official documentation provided by the US census \n", "# bureau we know consist of the year the survey was conducted,\n", "# the gender of the respondent, their age, their total income, \n", "# and their employment status.\n", "#\n", "# You can find the documenation for this data here: \n", "\n", "acs = acs[[\"year\", \"sex\", \"age\", \"inctot\", \"empstat\"]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's start to analyze our data. For starters, let's get warmed up by just getting the average income in our data:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1723646.2703978634" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acs[\"inctot\"].mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tada! Well that was easy. Except... that result says the average income in the US is... 1,723,646 dollars. And that's *clearly* not right—the United States is a very economically fortunate country, but the average American is definitely not a millionaire. \n", "\n", "OK, let's try something different—how about the overall employment rate? The person who gave you the data told you that people who are employed have `empstat` values of `\"employed\"`, so this should be easy too!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.46632017153389926" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(acs[\"empstat\"] == \"employed\").mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uh.... ok well the US employment rate is definitely not 47%. That'd imply an unemployment rate of 53%!\n", "\n", "So what's going wrong? At this point, there's no way to know, because we jumped to analyzing our data without ever getting to know it. So let's stop, take a step back, and get to know our ACS data before we start trying to analyze it. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting to Know Your Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whenever you get a new dataset, the first thing you probably want to do is just look at a few rows of the data! Seriously. Just look at it—it's amazing how often people skip this step. \n", "\n", "While there are many ways to do this, I personally recommend using the `.sample()` method, which will provide a random sample of rows from your data. By default, it only returns 1 row, but you can pass in the number of rows you want to get more (e.g., `.sample(5)` for five rows).\n", "\n", "Note that many people also like to use `.head()` (to get the first 5 rows of the data) or `.tail()` (to get the last 5). These are also fine tools to use, but be aware that because most data has been sorted in some way, the first or last 5 rows are unlikely to be *representative* of the data as a whole. Because `.sample()` gives you a random sample of rows, by contrast, it will give you a better sense of what the average row in your dataset looks like. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
yearsexageinctotempstat
1665902017male62170000employed
2078952017female69999999n/a
2145002017male180not in labor force
288632017femaleless than 1 year old9999999n/a
182802017female119999999n/a
\n", "
" ], "text/plain": [ " year sex age inctot empstat\n", "166590 2017 male 62 170000 employed\n", "207895 2017 female 6 9999999 n/a\n", "214500 2017 male 18 0 not in labor force\n", "28863 2017 female less than 1 year old 9999999 n/a\n", "18280 2017 female 11 9999999 n/a" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy.random as npr\n", "\n", "# The rows that `.sample()` picks will be random.\n", "# Normally that's advantageous, but in \n", "# this case it results in getting \n", "# a slightly different set of rows every time\n", "# I run this code, so I'm going to set a \n", "# seed—something we talked about in Course 2 \n", "# on numpy—so that the rows being picked \n", "# stay the same every time I run this code.\n", "npr.seed(42)\n", "\n", "acs.sample(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Right off the bat, even these few rows have taught us a lot. For example, we can see that at least in this sample, all of our data comes from 2017. We can also see how `sex` has been encoded (as `male` and `female`, not `M` and `F` or `Male` and `Female`), and that our data includes respondents from a wide range of ages including both adults and young children.\n", "\n", "We can also begin to see the causes of some of our problems. For example, it's not clear to see why `acs[\"inctot\"].mean()` didn't get what we wanted. Why? Because in five random rows, we've already gotten three people who report making 9,999,999 dollars, and all three are less than 12 years old. So clearly the variable doesn't just contain actual income data! \n", "\n", "(What are these `9999999` values? They're something called a \"Sentinel Value\"—a very old-school way of representing missing data, in which a special value that would never occur naturally was used to indicate when the field was \"missing.\" It was used back in the day when computers didn't use data types that had an actual special way of representing missing data, and some people still use it for backwards compatibility. We'll talk more about these and other ways of representing missing data in an upcoming reading.)\n", "\n", "Similarly, we can also see that while some observations of `empstat` make sense (`employed` or `not in labor force`), there are also values of `n/a` we need to figure out. `n/a` is a common shorthand for \"Not Applicable\", meaning it didn't make sense to ask the respondent that question. Since we see those appearing for respondents who are under 18, that probably means that `n/a` was filled in for all children, but that's something we'll have to investigate more. But it definitely tells us why we can't calculate the employment rate by running `(acs[\"empstat\"] == \"employed\").mean()`, since all the `n/a` values would pull down the share of respondents who are employed even though we don't consider children when calculating the employment rate!\n", "\n", "### Other Introspection Methods\n", "\n", "While looking at a sample of rows is helpful for getting a sense of our data, we of course use pandas (and statistics in general!) because most datasets are too big to look at and understand fully! So how do we get a better grasp of our data?\n", "\n", "The first pair of tools I would suggest are `.value_counts()` and `.describe()`. These are methods to tell you about *all* the values in a single variable. \n", "\n", "**.value_counts()**\n", "\n", "`.value_counts()`, for example, will show all the unique values a variable takes on *and* the number of occurrences of each value. For example, using `value_counts()` we can see that `empstat` takes on 4 values:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "employed 148758\n", "not in labor force 104676\n", "n/a 57843\n", "unemployed 7727\n", "Name: empstat, dtype: int64" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acs.empstat.value_counts()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And if we pass the keyword `normalize=True`, we can see the *proportion* of observations that take on each value (instead of the count):" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "employed 0.466320\n", "not in labor force 0.328134\n", "n/a 0.181324\n", "unemployed 0.024222\n", "Name: empstat, dtype: float64" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acs.empstat.value_counts(normalize=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "From this, we can see that in addition to the `n/a` observations that appear to be children, we also have two categories of people who aren't employed: `unemployed` and `not in labor force`. A little googling will tell us that `not in the labor force` connotes someone who is not looking for work, and is thus not considered unemployed when calculating the employment (or unemployment) rate—it turns out that the unemployment rate is only meant to tell us about the share of people *who want to work* who haven't found jobs, not the proportion of the overall population with a job.\n", "\n", "So now that we know that, we can calculate our unemployment rate by subsetting for people in those two categories, then calculating our average:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Employment Rate\n", "emp_rate = (acs.loc[acs[\"empstat\"].isin([\"employed\", \"unemployed\"]), \"empstat\"] == \"employed\").mean()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Or as we more commonly see it, the\n", "# UNemployment rate:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.04937853468383546" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 - emp_rate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Much better!\n", "\n", "Of course `.value_counts()` stops being useful when the number of unique values a variable takes on gets too big. For example, it isn't that useful to look at `.value_counts()` of total income, because, as we see below, it takes on over 8,000 unique values! Yes we can see there's a cluster at 0 and a cluster at our old friend `9999999`, but beyond that it isn't very helpful. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9999999 53901\n", "0 33679\n", "30000 4778\n", "50000 4414\n", "40000 4413\n", " ... \n", "70520 1\n", "76680 1\n", "57760 1\n", "200310 1\n", "505400 1\n", "Name: inctot, Length: 8471, dtype: int64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acs[\"inctot\"].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**.describe()**\n", "\n", "So for continuous numeric variables, we instead use `.describe()`, which provides summary statistics for a variable. Statistics is not required for this course, so not everything here will be familiar to all students, but just looking at the minimum value in the data (`min`), the maximum value in the data (`max`), and the the average value (`mean`) can tell you a lot:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "count 3.190040e+05\n", "mean 1.723646e+06\n", "std 3.732326e+06\n", "min -9.000000e+03\n", "25% 1.050000e+04\n", "50% 3.370000e+04\n", "75% 9.140000e+04\n", "max 9.999999e+06\n", "Name: inctot, dtype: float64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acs[\"inctot\"].describe()\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, in addition to our friend `9999999` being the max value, we can see that some people have total incomes of `-9000`. Now that *could* be a real value (maybe someone is in debt?), but we would want to check the documentation for the dataset to see if `-9000` has a special meaning in this case. (Note: turns out it's a real value, but I only know because I checked!)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "So that's a quick overview of why it's *so* important to explore your data before you charge ahead with your analyses. In particular, here we've shown the value of:\n", "\n", "- Checking a few random rows of your data with `.sample(5)`\n", "- Checking all the values of a variable with a limited number of discrete values with `.value_counts()`\n", "- Summarizing the distribution of a continuous variable with lots of values with `.describe()`.\n", "\n", "Now let's talk about how to fix problems when they crop up!" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 ('base')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "vscode": { "interpreter": { "hash": "3e0a5228cb9726a24d36227c69ed0d3aac98cecda769d1c9adb080711d57f90d" } } }, "nbformat": 4, "nbformat_minor": 4 }