# Sample Queries All queries assume you are in the `historysaid-global-economic-dataset/` directory. ## Get GDP for a specific country ### Python ```python import pandas as pd df = pd.read_parquet("data/unified/all_indicators.parquet") turkey_gdp = df[(df["country_code"] == "TUR") & (df["indicator_id"] == "wb.NY.GDP.MKTP.CD")] turkey_gdp = turkey_gdp.dropna(subset=["value"]).sort_values("year") print(turkey_gdp[["year", "value"]].tail(10)) ``` ### R ```r library(readr); library(dplyr) df <- read_csv("data/unified/all_indicators.csv", show_col_types = FALSE) df %>% filter(country_code == "TUR", indicator_id == "wb.NY.GDP.MKTP.CD", !is.na(value)) %>% arrange(year) %>% select(year, value) %>% tail(10) %>% print() ``` ## Compare an indicator across countries ### Python ```python countries = ["USA", "CHN", "IND", "BRA", "NGA"] inflation = df[(df["indicator_id"] == "wb.FP.CPI.TOTL.ZG") & (df["country_code"].isin(countries)) & (df["year"] == 2023)] print(inflation[["country_name", "year", "value"]].sort_values("value")) ``` ### R ```r df %>% filter(indicator_id == "wb.FP.CPI.TOTL.ZG", country_code %in% c("USA", "CHN", "IND", "BRA", "NGA"), year == 2023) %>% select(country_name, year, value) %>% arrange(value) %>% print() ``` ## Find all available indicators for a country ### Python ```python japan = df[df["country_code"] == "JPN"] indicators = japan.groupby(["indicator_id", "indicator_name"]).agg( years=("year", "count"), non_null=("value", "count") ).reset_index() print(indicators.sort_values("indicator_id")) ``` ### R ```r df %>% filter(country_code == "JPN") %>% group_by(indicator_id, indicator_name) %>% summarise(years = n(), non_null = sum(!is.na(value)), .groups = "drop") %>% arrange(indicator_id) %>% print(n = Inf) ``` ## Plot a time series ### Python ```python import matplotlib.pyplot as plt pop = df[(df["country_code"] == "DEU") & (df["indicator_id"] == "wb.SP.POP.TOTL")] pop = pop.dropna(subset=["value"]).sort_values("year") plt.plot(pop["year"], pop["value"] / 1e6) plt.title("Germany Population (millions)") plt.xlabel("Year") plt.ylabel("Millions") plt.grid(True) plt.tight_layout() plt.show() ``` ### R ```r library(ggplot2) df %>% filter(country_code == "DEU", indicator_id == "wb.SP.POP.TOTL", !is.na(value)) %>% ggplot(aes(x = year, y = value / 1e6)) + geom_line() + labs(title = "Germany Population (millions)", x = "Year", y = "Millions") + theme_minimal() ``` ## Check data coverage for an indicator ### Python ```python import json with open("_mappings/coverage_matrix.json") as f: coverage = json.load(f) for c in sorted(coverage, key=lambda x: x["fill_rate_pct"], reverse=True)[:10]: print(f"{c['indicator_id']}: {c['countries_with_data']} countries, {c['fill_rate_pct']}% fill rate") ``` ### R ```r library(jsonlite) coverage <- fromJSON("_mappings/coverage_matrix.json") coverage %>% arrange(desc(fill_rate_pct)) %>% head(10) %>% select(indicator_id, countries_with_data, fill_rate_pct) %>% print() ```