## Learning Objectives

- Read CSV files using pandas  
- Inspect and understand data structure  
- Clean column names and data types  
- Handle missing data  
- Visualize trends in cleaned data 
- Save cleaned datasets for reuse  
 

---

## Loading the Dataset

We’ll be using the Gapminder dataset.

```{python}
# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt

# Load data
df = pd.read_csv("../../data/gapminder.csv")

# Preview the dataset
df.head()
```

---

## Quick Overview of the Dataset

```{python}
df.shape  # number of rows and columns
```

```{python}
df.sample(5)  # random sample
```

---

## Inspecting the Data

Use pandas tools to understand the structure and quality of the data.

```{python}
df.info()
```

```{python}
df.describe()
```

```{python}
df.columns
```

---

## Filtering for Nigeria

Focus on data for Nigeria to keep things simple.

```{python}
nigeria = df[df["country"] == "Nigeria"].copy()
nigeria.head()
```

---

## Checking for Missing Data

Look for missing values — even if we expect none.

```{python}
nigeria.isnull().sum()
```

---

## Checking and Converting Data Types

Ensure each column uses the appropriate data type.

```{python}
nigeria.dtypes
```

Convert `year` to integer if needed:

```{python}
nigeria["year"] = nigeria["year"].astype(int)
```

---

## Creating a New Column

Calculate approximate total GDP: `population × gdp_per_capita`.

```{python}
nigeria["total_gdp"] = nigeria["pop"] * nigeria["gdpPercap"]
nigeria[["year", "gdpPercap", "pop", "total_gdp"]].head()
```

---

## Visualizing GDP per Capita Over Time

```{python}
nigeria.plot(x="year", y="gdpPercap", kind="line", title="GDP per capita in Nigeria")
plt.ylabel("gdpPercap")
plt.xlabel("Year")
plt.grid(True)
plt.tight_layout()
```

---

## Saving the Cleaned Dataset

Export the cleaned data for future use.

```{python}
nigeria.to_csv("../../data/gapminder_nigeria_cleaned.csv", index=False)
```

---

## Confirm File Save

Check that the file exists:

```{python}
import os
os.path.exists("../../data/gapminder_nigeria_cleaned.csv")
```

---


## Summary

In this session, we:

- Loaded and explored the Gapminder dataset  
- Filtered for Nigeria  
- Cleaned and renamed columns  
- Checked for missing data and adjusted types  
- Added a derived column (`total_gdp`)  
- Created a simple line plot  
- Saved a clean version for reuse  

---
