{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fd5e394b",
   "metadata": {},
   "source": [
    "#  Application Session 1\n",
    "## Colab interface, Pandas, first dataset (importing & basic inspection)\n",
    "\n",
    "**Session goal:**\n",
    "- Become familiar with the Google Colab interface (code cells, text cells, running cells, and outputs).\n",
    "- Learn how to load a dataset using Python\n",
    "- Inspect its structure and basic properties\n",
    "- Identify and apply a simple method to handle missing values\n",
    "- No statistical analysis or interpretation in this session\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e93b8480",
   "metadata": {},
   "source": [
    "### Step 1: Import Pandas library\n",
    "\n",
    "**Pandas** is a commonly used Python library for loading, organizing, and working with tabular data such as CSV and Excel files.  \n",
    "It is important in many real-world applications, including data analysis, engineering, finance, and machine learning, because it enables easy and organized data handling."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2b17e2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Data handling and tables\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "066db849",
   "metadata": {},
   "source": [
    "### Step 2: Define a small dataset\n",
    "- Define a simple dataset in CSV format to simulate reading a real data file.\n",
    "- CSV (Comma-Separated Values) is a simple text format widely used for storing data in tables, where each row represents an observation and each column represents a variable.\n",
    "Other commonly used data formats include Excel files (.xlsx), text files (.txt), and databases, which will be encountered in later python applications.\n",
    "- The dataset created here contains information for 11 students, including their study time and assessment results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0428d51a",
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_text = \"\"\"student_id,gender,hours_studied,quiz1,quiz2,score,date\n",
    "S01,M,0.5,3,2,49,2026-01-15\n",
    "S02,M,2,6,7,65,2026-01-18\n",
    "S03,F,3,7,7,70,2026-01-18\n",
    "S04,M,3,7,8,72,2026-01-19\n",
    "S05,F,4,8,,75,2026-01-19\n",
    "S06,M,4,8,9,78,2026-01-20\n",
    "S07,F,5,9,9,80,2026-01-20\n",
    "S08,M,5,9,10,82,2026-01-21\n",
    "S09,F,6,10,10,85,2026-01-21\n",
    "S10,M,7,10,10,88,2026-01-22\n",
    "S11,F,8,10,10,90,2026-01-22\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccd0bd45",
   "metadata": {},
   "source": [
    "### Step 3: Save the dataset to a CSV file\n",
    "This allows us to read the data as if it were an external file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbb65715",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"students.csv\", \"w\") as f:\n",
    "    f.write(csv_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f616982a",
   "metadata": {},
   "source": [
    "### Step 4: Load the CSV file into Python\n",
    "Use the `.read_csv()` method to load the CSV file into a Pandas DataFrame. Pandas converts raw data files (such as CSV files) into DataFrames, which organize data into rows and columns.\n",
    "This structure makes data easier to inspect, filter, summarize, and analyze compared to working with direct (raw) text or lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ea128b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(\"students.csv\")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0ee4f2f",
   "metadata": {},
   "source": [
    "### Step 5: Inspect the dataset structure\n",
    "A Pandas DataFrame (`df`) provides many built-in methods that help us inspect and understand the dataset structure, which is why Pandas is so widely used. So far in this session, we are using methods to check the size of the data, preview rows, inspect column information, and identify missing values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7926d13",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da0a6ff9",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a49a046",
   "metadata": {},
   "source": [
    "### Step 6: Preview the data\n",
    "Look at the first and last few rows (default is 5) to understand how the data is arranged."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91c387f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4da3847f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c6d6c61",
   "metadata": {},
   "source": [
    "### Step 7: Inspect categorical data\n",
    "Count how many observations fall into each category."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f70386f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['gender'].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac6cfbe0",
   "metadata": {},
   "source": [
    "### Step 8: Inspect numerical data\n",
    "Get a basic numerical summary for inspection only (no interpretation or analysis at this session)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f42b29d",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['score'].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c866fe8d",
   "metadata": {},
   "source": [
    "### Step 9: Check for missing values\n",
    "Identify columns that contain missing (NaN) values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95cf7861",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.isna().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e91c9c0e",
   "metadata": {},
   "source": [
    "### Step 10: Handle missing values (simple example)\n",
    "At this step, we apply a simple method to handle missing values by removing rows that contain missing data in a specific column. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ddf0f38",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a new DataFrame by removing rows with missing values in the quiz2 column\n",
    "df_drop = df.dropna(subset=[\"quiz2\"])\n",
    "\n",
    "# Display the updated dataset after removing missing values\n",
    "df_drop"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10a9f8a1",
   "metadata": {},
   "source": [
    "---\n",
    "### End of Session 1\n",
    "**Note:**\n",
    "- We perform these steps to prepare the data for statistical analysis later in the course.\n",
    "- We only *inspect* the data in this session.\n",
    "- Data analysis and visualization will be covered in later sessions.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
