I had an idea that I wanted to use the 2016 Summer Olympics medal results by event as an example for a pivot table in Excel. This is for a class that I teach, so it ultimately has to end up in Excel. Naturally, I made an attempt to scrape the data using Excel itself, but I ran into problems with that. So, I figured that I would use Mathematica to grab the data instead, partly as a learning exercise for myself.
Well, that turned out to be a bit more involved than I thought it would be. I ended up having to do a few different steps, and making heavy use of Part in order to massage the data into the form that I wanted. I often find Part to be confusing with nested lists, so this was a useful exercise. What I wanted was a list such as:
This list could then be exported to Excel for use in my pivot table. I have attached my notebook file for anyone to look at or use, if interested. I’m also open to (hoping for) some alternative ideas for how to accomplish the same result in an easier way.
EDIT: I found an error with the country association, so I uploaded a fixed version of the notebook.
Web scraping is more an art than a science, IMHO. Not sure Mathematica is a good tool to accomplish your goal. It is not a very good tool to do data munging, cleaning.
In your results, you may want to do some data review. I ran your notebook on OS X 10.10.5 with Mathematica 11.0 and in the resulting spreadsheet the following problems seem to have occurred:
Row 523
Row 829
Row 853
919 total rows including header row 1
which implies 919 medals awarded, however if I just use copy and paste on the following URL: | NBC Olympics I end up with a total medal count of 975 (notes: the resulting copy and paste yielded an error on row 26 for country Ukraine, the columns shifted left one column which I fixed manually)
Below are a set of steps in Excel only to get you to a spreadsheet that has correct number of awarded medals per event (note, I am off by 1 medal and my results need testing). The steps below deal with ties and related events in some events. My results at the intermediate step where I stopped are off by 1 metal in total count from the above count of 975.
These steps are all simple excel operations, result is spreadsheet of values, each row contains column for medal count for 1st, 2nd and 3rd.
To continue any further you will need to move to more advanced Excel Visual Basic code or move to a more powerful tool to find where the one metal was off, to pivot the data and continue to make it more tidy. However, it will become more difficult to parse programmatically as in few steps the athlete names are not parseable by ties, countries or team members.
Good hunting!
Data munging sucks then we die…
Steps to create the attached spreadsheet, done on OS X 10.10.5 Excel for Mac 2011 V14.6.6.
As I always say: “Excel should not be on the computer of a scientist/data scientist”. It seems to do some kind of job for people working in administration though - I am told.
Here a slightly different way of cleaning the data in Mathematica.
Thanks for your effort. I know how to get the data into Excel and manipulate it there. The problem is that I wanted a very specific format that is useful for a pivot table (as shown in my image), and without a bunch of manual “munging” it wasn’t going to happen. Also, the Get & Transform query that I was running on the Rio2016 website was occasionally crashing Excel. Even after getting the data with Mathematica, I still need to clean it up a bit in Excel due to some ties (there are sometimes several winners of a particular medal), but those are pretty easy to locate.
Thanks. Your solution is the kind of thing that I was looking for, though the final table doesn’t match the format that I need exactly (I need each medal on its own row). Your repeated use of Map is exactly the kind of thing that I need to practice on, so thanks for showing that solution.
Not that I was able to end up with a 100% correct solution either, but unfortunately for the swimmers in the women’s 100m freestyle and the men’s 100m butterfly the results are lost in your solution. Due to the corner case in these races where ties occurred for 1st place in the women’s race and silver in the men’s race.
I completely agree with your sentiment on the use of Excel. The original author’s goal was to create an Excel spreadsheet. My ugly solution was a poor example of using the targeted tool.
Scraping web data is a real challenge!
And, IMHO, cleaning scrapped data requires a robust visual inspection tool. While Mathematica 11 has made good steps to improve display of Datasets, it is still far short of a tool that is good for visual inspection.
I’ve got that (Mens 100m Butterfly). Also, I have double checked my medal counts by country against the Rio2016 site and they match, so I’m confident that I have the results presented accurately.
what a nice reference. I very much agree that in many areas, like biology, they do tend to use Excel. I do work at the interface with other areas of science and have experienced similar issues a lot.
Thanks a lot for that reference. I’ll make sure to use it…