Data sources¶
Exercise data-sources-annex.pdf · PDF
Anscombe's four datasets arrived on your screen ready to use: clean, tiny, already in a table. Somebody handed them to you.
Nobody will do that at work. Your boss will say "the sales are in the database", or forward you a spreadsheet, or point at a website. The data exist. They are just somewhere else, in some shape, behind some door. Before you can look at data, you have to go and get them.
That is stage 1 of the data lifecycle: obtain. In the crafting game, it is farming and looting. And every player knows the first question of looting: where is the loot, and what do I need to get in?
What you will learn
- Tell structured, semi-structured and unstructured data apart, with an example of each
- Recognise six kinds of data source and say what you need to reach each one
- Explain why a CSV file is not an Excel file, and what a separator is
- Say what open data is, and why "free" does not mean "no rules"
- Describe what Apache Superset is for, and where it came from
- Avoid the trap: forgetting that a source may ask for credentials before it lets you in
Three shapes of data¶
A data source is any place data come from: a database, a file, a web service.
Before asking where the data are, ask what shape they have. Here are the same two points of Anscombe's set I, stored three ways:
dataset,x,y
I,10,8.04
I,8,6.95
[
{"dataset": "I", "x": 10, "y": 8.04},
{"dataset": "I", "x": 8, "y": 6.95}
]
SELECT * FROM anscombe
WHERE dataset = 'I'
LIMIT 2;
Same data, three wrappers. The wrappers differ in how strict they are about shape:
| Shape | What it means | In the game | Examples |
|---|---|---|---|
| Structured | Fixed rows and columns. Every row has the same fields, in the same order. | The inventory grid: every slot has the same shape | A database table, a clean CSV file |
| Semi-structured | Every value has a label, but the shape can change from one record to the next. | A loot bag: each item is tagged, but no two bags hold the same things | JSON, XML |
| Unstructured | No fields at all. A person understands it; a program cannot just read "the price column". | A screenshot of your base | Photos, audio, a PDF of a letter, a WhatsApp chat |
Why is JSON only semi-structured? Because a record may have a field that the next one lacks, and nobody complains:
[
{"pokemon": "bulbasaur", "type_1": "grass", "type_2": "poison"},
{"pokemon": "charmander", "type_1": "fire"}
]
In a table, the second row would still need something in the type_2 column. In JSON, the label is simply not there.
Careful: JSON is not unstructured
People see curly brackets and nested text and call it "unstructured". It is not: every value carries a label ("type_1": "fire"), so a program can find it. Unstructured means no labels at all, like a photo. JSON sits in the middle.
Charts need rows and columns. So this course works with structured data, and when a source hands you something else, part of the job is turning it into a table first.
Six places where data live¶
Now the looting map. These are the six kinds of source you will meet in this module and in almost any job. The last column is the one that matters most.
| Source | Shape | Everyday example | In the game | What you need to reach it |
|---|---|---|---|---|
| Relational database | Structured | A school's student records; a shop's orders | A vault on a server | Host, port, database name, user and password, and a driver |
| CSV file | Structured | An export from a banking app; Anscombe's quartet | An item lying on the ground | Just the file. Know its separator and its encoding. |
| Excel file | Structured, more or less | The spreadsheet your boss emails you | A chest somebody packed by hand | Just the file. Pick the right sheet; beware merged cells. |
| JSON file | Semi-structured | What a weather app downloads | A loot bag | The file or its web address. Know how it is nested. |
| API | Usually JSON | Spotify, a bus timetable, a weather service | A trader: you ask, it answers | A web address, often an API key. Respect the rate limit. |
| Open-data portal | Any of the above | A government data website | A public chest in the town square | Nothing: public on purpose. Check the licence. |
Six new terms in one table. One at a time:
- A relational database stores data in tables that can be linked to each other. It runs as a program on a server, such as PostgreSQL or MySQL, and you talk to it in a language called SQL. You will write your first SQL on the next page.
- The host, the port and the driver are the address, the door and the translator you need to reach a database. They get a full page: Connecting Superset to a database.
- An API (application programming interface) is a web address that answers with data instead of a web page. Your weather app does not store the forecast: it asks an API.
- An API key is a password for programs: a long code the service gives you so it knows who is asking.
- A rate limit is the maximum number of questions an API accepts in a given time. Ask too often and it stops answering for a while.
- A licence is the text that says what you are allowed to do with data somebody else published.
Who asks for credentials?¶
Credentials are what you show to prove who you are: usually a user name and a password, or an API key.
flowchart LR
Q{"Does the source<br>ask who you are?"}
Q -- "always" --> DB["<b>Relational database</b><br>user + password"]
Q -- "usually" --> API["<b>API</b><br>API key"]
Q -- "never" --> F["<b>Files</b><br>CSV, Excel, JSON:<br>if you have the file, you are in"]
Q -- "never" --> OD["<b>Open-data portal</b><br>public on purpose"]
A database always asks. An API usually asks. A file never asks: whoever holds the file holds the data. That is convenient, and it is also why you do not email a file with customers' personal details as casually as a meme.
Careful: \"I have the address, so I am in\"
Knowing where a database is does not open it. You need its address and an account on it and the permission to read the tables you want. Access is the part that people forget, and it is the first thing that goes wrong in real projects.
CSV is not Excel¶
Both open in a spreadsheet program with a double click. That is where the confusion starts.
A CSV file (comma-separated values) is a table written as plain text: one line per row, with a character between one value and the next.
That character is the separator (also called the delimiter). The first line usually holds the column names: it is the header. This is the real beginning of pokemon_df.csv, the dataset we use in this unit, shortened to its first eight columns:
id,pokemon,species_id,height,weight,base_experience,type_1,type_2
1,bulbasaur,1,0.7,6.9,64,grass,poison
2,ivysaur,2,1,13,142,grass,poison
Nothing hidden. What you see is the whole file.
An Excel file is a different animal:
CSV (.csv) |
Excel (.xlsx) |
|
|---|---|---|
| What it really is | Plain text | A ZIP package with several XML files inside |
| Open it in a text editor | You can read every value | Unreadable symbols |
| Sheets | One table | Many sheets in one file |
| Formats, colours, formulas | None. Values only. | All of them |
| Who can read it | Almost every program ever written | Programs that understand the format |
| Typical surprise | The wrong separator | Merged cells, titles above the table, totals at the bottom |
Prove it yourself
Open a .csv file and a .xlsx file in a plain text editor, not in a spreadsheet. The CSV is readable line by line. The Excel file is a wall of symbols, because you are looking at a compressed package.
The separator trap¶
The "C" in CSV promises commas. It does not always keep the promise.
In Spain, as in much of Europe, the decimal mark is a comma: we write 6,9 kg. If the separator were also a comma, 6,9 would look like two values. So files made in these countries often use a semicolon as separator instead:
pokemon,height,weight
bulbasaur,0.7,6.9
pokemon;height;weight
bulbasaur;0,7;6,9
Both are valid CSV files. A tool that expects one and receives the other will read every line as one single column, or split your decimals in two. The file is fine. The tool was told the wrong thing.
There is a second hidden setting: the encoding, the rule that turns letters into the bytes stored in the file. When a tool guesses it wrong, Calvià shows up as CalviÃ. If you see that, the data are not broken: the encoding is wrong.
So, one habit for life: before you load a file anywhere, open it in a text editor and look at the first lines. You are checking the separator, the decimal mark and the header. Mismatched formats are a UT2 problem, but you spot them today.
Open data: free, not lawless¶
Open data are data that an organisation publishes so that anyone can download, reuse and share them.
Governments publish open data: budgets, bus lines, beaches, school enrolment. The data community does too. Two sources you will use this year:
| Source | Who publishes it | What you find |
|---|---|---|
| GOIB open-data portal | The Government of the Balearic Islands | Tourism, transport, environment, education |
| TidyTuesday | A volunteer data community | A new public dataset every week since 2018: music, games, films, sport |
Our Pokémon dataset comes from TidyTuesday. More sources, with direct links to every class file, are on the Resources page.
Careful: open does not mean \"do whatever you want\"
Open data are free to use, but they still come with a licence. A very common one is CC BY (Creative Commons Attribution): you may copy the data, change them and even use them commercially, as long as you credit the source. So before using a dataset in your project, find its licence, and write where the data came from in your report.
Meet Apache Superset¶
Six kinds of source, three shapes, separators, credentials. You need a tool that can open those doors and show you what is behind them.
Apache Superset is a web application for exploring data: it connects to databases, lets you ask them questions in SQL, and turns the answers into charts and dashboards.
In the game, Superset is the client: the window you look through. Remember that for the next page, because it has a consequence: Superset does not store your data. It connects to the database where they live, asks, and draws.
flowchart LR
C["<b>Connect</b><br>to a database"] --> Q["<b>Query</b><br>ask in SQL"]
Q --> V["<b>Chart</b><br>draw the answer"]
V --> D["<b>Dashboard</b><br>several charts<br>on one screen"]
Its story is short. In 2015, at a hackathon (an event where programmers build a prototype in a few days) inside Airbnb, an engineer called Maxime Beauchemin built a tool to slice and chart the company's data quickly. He had already created another well-known data tool, Apache Airflow. Airbnb released the new tool as open source: its code is public, and anyone may use it, study it and improve it for free.
| Year | What happened |
|---|---|
| 2015 | Born as a hackathon project at Airbnb, by Maxime Beauchemin |
| 2017 | Enters the incubator of the Apache Software Foundation, a non-profit that looks after open-source projects |
| 2021 | On 21 January, Apache announces Superset as a top-level project: it has graduated from the incubator |
It is the main tool of this module. Near the end of this unit you will also meet its best-known commercial rival, Microsoft's Power BI, and compare how the two get their data.
Try it: an open-data hunt¶
You need a browser and a text editor. No account, no password.
| Step | Do this | You should see |
|---|---|---|
| 1 | Go to the Resources page and download pokemon_df.csv |
A file of about 200 KB |
| 2 | Open it in a text editor, not in a spreadsheet | A header line, then one line per Pokémon |
| 3 | Find the separator and the decimal mark | A comma, and a decimal point |
| 4 | Count the columns in the header | 22 |
| 5 | Look at the number of the last line of text in your editor | 950: one header and 949 Pokémon |
| 6 | Find charmander and read its type_2 value |
The two letters NA: this file's way of saying "no value" |
Now do the same with a dataset you choose, from the GOIB portal or from TidyTuesday, and fill in this card:
| Field | Your answer |
|---|---|
| Title and web address | |
| Formats offered | CSV, JSON, XLSX... |
| Shape | Structured or semi-structured |
| Separator and decimal mark | |
| Rows × columns, roughly | |
| Licence | |
| One question it could answer | For example: "Are the most popular songs also the most danceable?" |
Keep that last line. A good question is the start of every chart you will draw this year.
Key takeaways¶
- Data come in three shapes: structured (fixed rows and columns), semi-structured (labelled, flexible, like JSON) and unstructured (no fields, like a photo).
- Six kinds of data source: relational database, CSV, Excel, JSON, API and open-data portal. For each one, ask: what do I need to get in?
- Credentials: a database always asks for them, an API usually does, a file never does.
- A CSV is plain text with a separator; an Excel file is a ZIP package of XML. Open files in a text editor before loading them, and check separator, decimal mark and header.
- Open data are free to reuse, under a licence. With CC BY, you must credit the source.
- Apache Superset connects to databases, queries them in SQL and draws charts and dashboards. It began at Airbnb in 2015 and has been a top-level Apache project since 2021.
Quick check¶
1. A JSON file lists Pokémon. Some records have a type_2 field and some do not. What shape is this data?
- Structured
- Semi-structured
- Unstructured
- Relational
Every value has a label, but the records do not all have the same fields: that is semi-structured. Structured data would force every row to have the same columns; unstructured data would have no labels at all.
2. Which kind of source always asks for credentials before it lets you in?
- A CSV file
- A relational database
- An open-data portal
- An Excel file
A relational database always wants a user and a password, on top of host, port, database name and a driver. Files never ask (whoever holds the file holds the data), and open-data portals are public on purpose.
3. You open a file in a text editor and see unreadable symbols instead of rows and values. Which file is it most likely to be?
- A CSV file with a semicolon separator
- A JSON file
- An Excel
.xlsxfile - A CSV file with the wrong encoding
An .xlsx file is a ZIP package of XML files, so a text editor shows compressed gibberish. CSV and JSON are plain text: readable, even when the separator or the encoding is not the one you expected.
4. You load a CSV from a Spanish public portal and the whole table arrives as one single column. What is the most likely cause?
- The file is damaged and must be downloaded again
- The file has too many rows
- The file uses a semicolon as separator and the tool expected a comma
- The file needs a password
Where the decimal mark is a comma, files often use a semicolon separator. A tool told to split on commas finds none between the values, so each line stays in one piece. The file is fine: change the separator setting.
5. A dataset is published under a CC BY licence. What does that ask of you?
- Nothing: open data have no rules
- To pay before using the data commercially
- To credit the source when you use the data
- To ask for written permission each time
Open means free to reuse, not lawless. CC BY lets you copy, change and even sell work based on the data, on one condition: attribution, saying where the data came from.
6. What does Apache Superset do?
- It stores your data so that other programs can read them
- It connects to databases, asks them questions in SQL and draws the answers as charts and dashboards
- It converts Excel files into CSV files
- It publishes open data for governments
Superset is the client: it connects, queries and draws. The data stay in the database. It started at Airbnb in 2015 (Maxime Beauchemin) and is open source.
7. A gym wants a screen showing how many people train each hour. The data are in the booking app's MySQL database and in a weekly Excel file from reception. Pick the two statements that are true.
- For the MySQL database you need host, port, database name, user, password and a driver
- For the Excel file you need an API key
- For the Excel file you only need the file itself, but you must check which sheet holds the data
- The MySQL database is semi-structured, so it cannot be turned into a chart
A relational database (MySQL is one) is structured and always asks for the full set of access details. An Excel file asks for nothing but the file: your work there is picking the right sheet and watching out for merged cells. API keys belong to APIs.
You know where data live and what each place asks for. Time to open the hardest door of the six, the database: Connecting Superset to a database.