23 June 2020

Wolfram Select Statement

I have been using Wolfram Mathematica to visualise some data around COVID-19. Selecting a subset of rows from a DataSet proved frustrating until I realised the field I was trying to select did not contain a string.

The Wolfram Notebook I was using can be found at https://www.wolframcloud.com/obj/admoss0/Published/Covid.nb

Wolfram language has some very powerful constructs. The following statement grabs a csv file from the Internet, works out what all the datatypes should be and stores it in a DataSet object named wdata.

wdata = SemanticImport [
data/owid-covid-data.csv “]

All good so far. The wdata object contains a huge amount of data. It has 25497 rows and 34 columns. I only wanted the new_cases column for a single country. Sounds easy. Until you try. After browsing the web about it I tried:

AUcases = wdata[Select[#location == “Australia”&], {“date”,”new_cases”}];

But that would have been too easy. It didn’t work. Yet example after example on the web indicated it should. The problem? It turns out the contents of the location field was not a string. It was an object.

screen capture of the line from Wolfram Desktop

It took 4 days and much of 4 nights to discover this. If you look closely the word Australia is inside a little orange box. It isn’t text. It isn’t a string. It is a Wolfram language COUNTRY object.

SemanticImport is too smart. It recognised the entries in the location field referred to countries and rather than just storing the name as a string, it stored a country object in the DataSet.

This is immensely useful for further processing. That country object contains all sorts of useful information, like population, GDP, you name it.

But until this one fact is known, that the location field does not contain strings, trying to select using a string for the country is maddening.

The Solution

Rather than surrounding the word Australia with “quotes” you press <CTRL>= and the Wolfram Desktop pops up a box for natural language input. Type Australia into that box and Wolfram checks Wolfram Alpha to see what it knows about it. It correctly works out Australia is a country and provides a country object. This matches the country object in the location field of the DataSet and magically the Select statement works!

Copyright 2020. All rights reserved.

Posted June 23, 2020 by David Moss in category "Tech

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.