Working with data from start to finish (Part 3)

This is Part 3 of a 3-part series on data science in name, theory and practice.

In Part 2 of this series, I discussed a systematic process for making sense out of data, without which it is easy to fall into the trap of drawing unactionable - or worse - unreliable conclusions. With the framework established, the goal of this post is to then walk through some of the code (in Python) required to implement it. I'll use the IGM Economic Experts Panel dataset, which catalogs the responses of dozens of economists on public policy issues - you can see the results of my analysis here.


Since there's no option to download this data, the first step is figuring out which data to scrape. Here's what the website looks like:

Below this initial landing survey are links to all the other surveys:

It's within these surveys (ie. each link) that I find the actual data: tables listing each economist, their vote, their confidence and any comments they have.

So the roadmap is: scrape the homepage for the links to each survey, then scrape the response tables from each survey.

Now, how do I get the links to each survey? Ideally, all the links follow the same pattern. To check this, I inspect the DOM by right clicking on the link in Chrome and selecting "Inspect" (you can also view source with CTRL+U or F12). A frame will pop up that looks like this:

Here I see a pretty discernible pattern: each link is denoted by the <a> tag, nested within a <h2> header tag, wrapped in a <div> with an attribute of class equal to poll-listing (I end up using a slightly different pattern in the code). It's good practice to isolate with a unique pattern - after all, there may be links within header tags whose wrapping <div>s do not have the poll-listing class. 

Now that I know what to look for, I can write the code. Logically, I want to traverse the DOM and store each link in a collection (eg. list, tuple, dictionary).

I use requests to pull the DOM of the webpage, Next, I parse the DOM using BeautifulSoup - essentially this gives me a shortcut to isolating certain elements (eg. links, tables, etc.), without which I'd have to structure myself. Using my BeautifulSoup object (in this case, the soup variable), I isolate all the <div> elements whose class attribute equals poll-listing. However, so far .findAll() has only given me an iterable ResultSet object, not the actual links I'm looking for. Therefore, I iterate over each Tag object (the poll variable) and use poll.find('h3', {'class': 'surveyQuestion'}).a['href'] to get the hyperlink as a string. I then store the link in the topics dictionary. Naturally, when using a new and unfamiliar package, I check the documentation religiously to understand all the methods which can make my job easier.

So after all that, I have the the links in a dictionary. Now I can iterate over each link, and for each page, scrape the tables on those pages. The resulting code is similar:

First, I declare a data list object because pandas (the package for data tables) accepts data most easily as a list of dictionaries. Each element in the list is a row, and for each element, the dictionary's keys represent the columns. As I scrape each row from the tables, I put each cell into a dictionary, and each dictionary is appended to the data list. 

So given the list of links for each particular survey, I again use requests to pull their webpages, then BeautifulSoup to isolate the parts I need - in this case the <table> elements. (The line with list('ABCD') basically says: assign a letter to each individual survey because the poll page can include one or multiple surveys).

Now for the next part of the code - what do I do with these tables?

The first line essentially iterates over each table on the webpage, with a slight adjustment of tying the assigned letter to its respective table. Now, for each table, I need to pull all the rows (<tr> tags). For each row, pull (from left-to-right) the data in each cell (<td> tags). Retrieve and clean the actual text data from each cell (.get_text()), then put it in a dictionary with some sensible column names. Finally, append the dictionary (row) to the data list. Again, for some perspective, the data list will contain rows from every table on every poll webpage, so it's important to make each row unique by including columns for the survey question (eg. A, B, etc.) and poll URL.

At last, I've scraped the data from all of the surveys! The last scraping step is to temporarily store the data in a more amenable format:

For most datasets, you can persist this data (ie. a permanent format on disk, not just RAM) in a Python-specific pickle file or .csv:

df.to_pickle(os.path.join(os.path.dirname(__file__), 'igmchicago.pkl')

However, for some datasets, you'll want use a relational database.


pandas makes it easy to dump a dataframe into SQL, without which you'd have to use raw SQL (ie. building the schema and inserting the data) - a much more painful process. 

Once your data is in a database, you can run plain-old SQL on it. For example, which economists have average confidence levels higher than the average for their institution?

While I have a strong preference for fast, simple data manipulation in pandas, SQL is an absolute must for any data/BI analyst.


After persisting the data, I need to understand what's in it. What are the range of categorical variables and what do they indicate? Do some categories mean the same thing? Are there null values? How many rows are there and how many would I expect? I virtually always use the following commands to quickly get a sense of the data:

EDA will accomplish two things: (1) you will learn the correct interpretation of your data and (2) you will identify some (presumably not all) errors in the data which need to be corrected. In this case, I noticed a couple of issues with my original scraped data.


First, I saw that virtually all the recently added economists had very low survey response rates. I checked into the original data and noticed that although these economists were given the opportunity to retroactively respond to surveys, these responses were included as a second row, not the initial row which I scraped. 

So I had to go back through my scraping code and rescrape the second row if it had data in it. After doing this, I confirmed that the discrepancy in survey response rates between newly added economists and all other economists was removed. Success!

Second, all comments were stored as a string, even if they were blank (ie. an empty string: ""). The summary statistics therefore would lead one to believe that every economist always left a comment, which was obviously untrue. As a result these empty strings needed to be recoded as null values.

Finally, I decided that adding the sex of economists would be useful and coding the response categories as numeric values would facilitate aggregation.


Data analysis is often the meat of any data-driven project: you've gathered, stored, understood and validated the data, now how can you use it to answer business or research questions?

I did not thoroughly analyze this dataset, though there is a lot I could do. I could run a regression with dummy variables and see if sex was associated with more or less confidence in responses. I could find the average confidence by institution, or tabulate an economist's confidence level over time. In many cases, I don't need more than intermediate statistics. A lot of insight can be gained by simply looking at any metric by a dimension (eg. region, sales channel, etc.), over time, or as a proportion of the total. 

Here are some quick summary statistics I ran with pandas


I am a strong advocate of visualizing everything. Your audience will rarely be statisticians or analysts - often, they are decision-makers who simply need to know the next step and want a quick way of seeing the data-driven evidence. Not only that but visualization will quickly show you if your results seem counter-intuitive, allowing you the chance to review your data and analyses before presenting them.

Unlike R, which has the powerful and intuitive ggplot2, Python does not feature as robust plotting libraries. The most popular packages I have seen are seabornggplot (ported from R), bokeh (displays in the browser), and the low-level matplotlib. In my opinion, none of these are as mature as ggplot2 due to either bugs or lack of features. While working on this project, I toyed with each library but eventually settled on matplotlib to visualize exactly what I needed - of course, at the cost of concise code and readability. 

For the sake of brevity, I decided not to include the code to visualize the data here, but you can view the graphs here and the code on GitHub.

That wraps up a high-level overview of implementing the data pipeline from start to finish. I strongly believe that once you understand the main steps in the process, most analytical projects become feasible. If you need new data, or have to clean data, or need to store and query large amounts of data, or visualize data, all you need is an understanding of these basic principles.

Hope you enjoyed reading! This post concludes the 3-part series on data science in name, theory and practice.

Full code for this project can be found on GitHub.