From Statements to Tables
DS 2023 | Communicating with Data
Statements and atomic facts¶
Consider the list of statements for ten countries.
France has the capital Paris.
France has a population of about 68 million.
France has the currency the euro.
France has a land area of about 551,700 square kilometers.
Japan has the capital Tokyo.
Japan has a population of about 123 million.
Japan has the currency the yen.
Japan has a land area of about 378,000 square kilometers.
Brazil has the capital Brasília.
Brazil has a population of about 213 million.
Brazil has the currency the Brazilian real.
Brazil has a land area of about 8,516,000 square kilometers.
Egypt has the capital Cairo.
Egypt has a population of about 108 million.
Egypt has the currency the Egyptian pound.
Egypt has a land area of about 1,002,000 square kilometers.
Australia has the capital Canberra.
Australia has a population of about 27 million.
Australia has the currency the Australian dollar.
Australia has a land area of about 7,692,000 square kilometers.
Canada has the capital Ottawa.
Canada has a population of about 41 million.
Canada has the currency the Canadian dollar.
Canada has a land area of about 9,985,000 square kilometers.
India has the capital New Delhi.
India has a population of about 1.45 billion.
India has the currency the Indian rupee.
India has a land area of about 3,287,000 square kilometers.
Germany has the capital Berlin.
Germany has a population of about 84 million.
Germany has the currency the euro.
Germany has a land area of about 357,600 square kilometers.
Kenya has the capital Nairobi.
Kenya has a population of about 56 million.
Kenya has the currency the Kenyan shilling.
Kenya has a land area of about 580,400 square kilometers.
Mexico has the capital Mexico City.
Mexico has a population of about 131 million.
Mexico has the currency the Mexican peso.
Mexico has a land area of about 1,964,000 square kilometers.
These are all atomic facts. Each is an expression of a sementic triple: A Subject, a Predicate, and an Object.
In order to emphasize this three-part structure, we may rewrite them in the following way:
France hasCapital Paris
France hasPopulation 68000000
France hasCurrency Euro
France hasLandArea 551700
Japan hasCapital Tokyo
Japan hasPopulation 123000000
Japan hasCurrency Yen
Japan hasLandArea 378000
Brazil hasCapital Brasilia
Brazil hasPopulation 213000000
Brazil hasCurrency BrazilianReal
Brazil hasLandArea 8516000
Egypt hasCapital Cairo
Egypt hasPopulation 108000000
Egypt hasCurrency EgyptianPound
Egypt hasLandArea 1002000
Australia hasCapital Canberra
Australia hasPopulation 27000000
Australia hasCurrency AustralianDollar
Australia hasLandArea 7692000
Canada hasCapital Ottawa
Canada hasPopulation 41000000
Canada hasCurrency CanadianDollar
Canada hasLandArea 9985000
India hasCapital NewDelhi
India hasPopulation 1450000000
India hasCurrency IndianRupee
India hasLandArea 3287000
Germany hasCapital Berlin
Germany hasPopulation 84000000
Germany hasCurrency Euro
Germany hasLandArea 357600
Kenya hasCapital Nairobi
Kenya hasPopulation 56000000
Kenya hasCurrency KenyanShilling
Kenya hasLandArea 580400
Mexico hasCapital MexicoCity
Mexico hasPopulation 131000000
Mexico hasCurrency MexicanPeso
Mexico hasLandArea 1964000Now, you can see how these facts can be mapped onto a table:
Subjects row names
Predicates column names
Objects cell values
| Subject | hasCapital | hasPopulation | hasCurrency | hasLandArea |
|---|---|---|---|---|
| France | Paris | 68000000 | Euro | 551700 |
| Japan | Tokyo | 123000000 | Yen | 378000 |
| Brazil | Brasilia | 213000000 | BrazilianReal | 8516000 |
| Egypt | Cairo | 108000000 | EgyptianPound | 1002000 |
| Australia | Canberra | 27000000 | AustralianDollar | 7692000 |
| Canada | Ottawa | 41000000 | CanadianDollar | 9985000 |
| India | NewDelhi | 1450000000 | IndianRupee | 3287000 |
| Germany | Berlin | 84000000 | Euro | 357600 |
| Kenya | Nairobi | 56000000 | KenyanShilling | 580400 |
| Mexico | MexicoCity | 131000000 | MexicanPeso | 1964000 |
A more complex example¶
Let’s go through this process again, with a slightly more complex example.
Consider the following descriptive statement, a sentence you might find in a report on global health.
“In 1970, Germany spent $USD 252.311 per capita and had a life expectany of 70.6 years.”
Now, this sentence can be decomposed into a set of atomic facts.
We might write down these facts like this:
Observation1 country Germany
Observation1 year 1970
Observation1 spending 252.311
Observation1 life_expectancy 70.6Note that in writing these doown we have surfaced something interesting -- the implicit subject of the sentence is the observation.
This is a little tricky, because it looks like the the subject is the country. But the year 1970 is not a property of Germany!
Now, imagine we have a bunch of these sets of facts, one set for each country in the world and year between 1970 and 2020.
A segment of these data might look like the following multi-line Python string:
data = """
Observation1 country Germany
Observation1 year 1970
Observation1 spending 252.311
Observation1 life_expectancy 70.6
Observation2 country France
Observation2 year 1970
Observation2 spending 192.143
Observation2 life_expectancy 72.2
Observation3 country GreatBritain
Observation3 year 1970
Observation3 spending 123.993
Observation3 life_expectancy 71.9
Observation4 country Japan
Observation4 year 1970
Observation4 spending 150.437
Observation4 life_expectancy 72.0
Observation5 country USA
Observation5 year 1970
Observation5 spending 326.961
Observation5 life_expectancy 70.9
"""Putting the facts into a table¶
Let’s use Pandas to convert these facts into a table
We’ll use a list comprehension to parse the string into a list of lists and pass that to a data frame constructor.
lol = [row.split() for row in data[1:-1].split("\n")]
lol[['Observation1', 'country', 'Germany'],
['Observation1', 'year', '1970'],
['Observation1', 'spending', '252.311'],
['Observation1', 'life_expectancy', '70.6'],
['Observation2', 'country', 'France'],
['Observation2', 'year', '1970'],
['Observation2', 'spending', '192.143'],
['Observation2', 'life_expectancy', '72.2'],
['Observation3', 'country', 'GreatBritain'],
['Observation3', 'year', '1970'],
['Observation3', 'spending', '123.993'],
['Observation3', 'life_expectancy', '71.9'],
['Observation4', 'country', 'Japan'],
['Observation4', 'year', '1970'],
['Observation4', 'spending', '150.437'],
['Observation4', 'life_expectancy', '72.0'],
['Observation5', 'country', 'USA'],
['Observation5', 'year', '1970'],
['Observation5', 'spending', '326.961'],
['Observation5', 'life_expectancy', '70.9']]import pandas as pd
df = pd.DataFrame(lol, columns=['Subject','Predicate','Object'])
dfReshaping the table¶
Now, the shape of this table is not how we normally want our data.
It shows how the data set is composed of facts, but it’s not useful in this form for data analysis.
For one thing, the Object column contains data of different types, which prevents storing them and processing them properly.
One thing we can do is pivot the table into this form:
df1 = df.pivot(index='Subject', columns='Predicate', values='Object')
df1We see that that the pivot operation sorted the columns alphatically.
A more intuitive way of represented the data is as follows:
df1[['country','year','spending','life_expectancy']]In this version of the table, the columns show the common pattern of the descriptive sentence: “Country in year spent dollars per capita and had a life expectancy of .” The sentence encodes a hypothesized cause and effect relationship: what factors influence life expectancy?
Summary¶
We have seen that a table can be constructed from a collection of atomic facts.
Each atomic fact has a three-part structure: Subject, Predicate, Object.
The subject of an atomic fact is often observation itself.
These parts can be mapped onto a table as rows, columns, and cells.
Tables are a primary form of visualizing linguistic information.
Challenge¶
Generate a collection of atomic facts by describing something and collecting the data in a spreadsheet.
Then convert the data in the spreadsheet to a table where rows are subjects and columns are predicates.