{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8b2b445",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">Pandas คืออะไร</h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">Pandas คือไลบรารี (Library) ของภาษา Python แบบโอเพนซอร์สที่ได้รับอนุญาตภายใต้ BSD License ซึ่งมีโครงสร้างข้อมูลสำหรับการวิเคราะห์และจัดการข้อมูลที่มีประสิทธิภาพสูง ใช้งานง่าย สำหรับภาษา Python ที่มีความหลากหลายในการใช้งาน</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">ไลบรารี Pandas มีโครงสร้างข้อมูลที่ใช้จัดเก็บข้อมูลในรูปแบบตาราง เรียกว่า Series และ DataFrame ด้วยโครงสร้างข้อมูลเหล่านี้ คุณสามารถปฏิบัติงานต่าง ๆ เช่น การกรอง (Filtering) การรวมข้อมูล (Merging) และการปรับแต่งข้อมูลตามเกณฑ์ต่าง ๆ ได้</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">Pandas สามารถนำเข้าข้อมูลจากแหล่งที่มาที่หลากหลาย เช่น ไฟล์ธรรมดา (Flat files ได้แก่ CSV, Excel ฯลฯ) ฐานข้อมูล และแม้กระทั่งลิงก์ออนไลน์ นอกจากนี้ Pandas ยังมีฟังก์ชันการแสดงภาพข้อมูล (Data Visualization) โดยสามารถพลอตกราฟประเภทต่าง ๆ ได้</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">ข้อได้เปรียบหลักบางประการของไลบรารี Pandas ในภาษา Python</p>\n",
    "\n",
    "<ul>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">Pandas มีคุณสมบัติในตัวที่ช่วยจัดการชุดข้อมูล (Datasets) ขนาดใหญ่มากได้อย่างรวดเร็วและใช้หน่วยความจำอย่างเหมาะสม (memory-optimized)</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">ไลบรารี Pandas มาพร้อมกับฟีเจอร์เริ่มต้นมากมายสำหรับงานด้านวิทยาศาสตร์ข้อมูล (Data Science) และการเรียนรู้ของเครื่อง (Machine Learning) </div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">ไลบรารี Pandas สามารถทำงานร่วมกับแหล่งข้อมูลนำเข้าที่หลากหลาย</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">Pandas ใช้การจัดทำดัชนีแบบมีป้ายกำกับ (labeled indexing) สำหรับระเบียน (Records) ซึ่งเป็นคุณสมบัติที่เข้าใจง่ายอย่างยิ่งสำหรับการแบ่งกลุ่ม (segmenting) และการแยกชุดข้อมูลย่อย (subsetting) จากชุดข้อมูลขนาดใหญ่</div></li>\n",
    " \n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d4541d3",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">Pandas Series</h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">Pandas Series คือโครงสร้างข้อมูลที่ใช้จัดเก็บข้อมูลในรูปแบบของ คอลัมน์ หนึ่งคอลัมน์ มักจะถูกใช้เพื่อจัดเก็บข้อมูลเกี่ยวกับคุณลักษณะเฉพาะ (Attribute) อย่างใดอย่างหนึ่งในชุดข้อมูล (Dataset)</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d4ebc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "my_array = np.array([10, 20, 30, 40, 50])\n",
    "\n",
    "my_series = pd.Series(my_array)\n",
    "print(my_series)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3d1dd9e",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">จากตัวอย่างข้างต้นจะเห็นได้ว่า index ของ Series เริ่มต้นจาก 0 ในตัวอย่างต่อไปจะเป็นการกำหนดดัชนีขึ้นมาใหม่ด้วยตัวเอง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cab0b2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_array = np.array([10, 20, 30, 40, 50])\n",
    "\n",
    "my_series = pd.Series(my_array, index=[\"num1\", \"num2\", \"num3\", \"num4\", \"num5\"])\n",
    "print(my_series)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37e975a1",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในตัวอย่างต่อไปจะเป็นการสร้าง series โดยใช้ตัวเลข</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a6bcacf",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_series = pd.Series(30, index=[\"num1\", \"num2\", \"num3\", \"num4\", \"num5\"])\n",
    "print(my_series)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90f7e31b",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ตัวอย่างการสร้าง series โดยใช้ dictionary</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6421f7db",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_dict = {'num1':6, 'num2':7, 'num3':8}\n",
    "\n",
    "my_series = pd.Series(my_dict)\n",
    "print(my_series)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3657e6f",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การดำเนินการใน Pandas Series</h1>\n",
    "    \n",
    "<p style=\"font-size: 16px; line-height:2.0;\">การดำเนินการสำหรับ Pandas series มีหลายแบบ ตัวอย่างแรกที่เราจะศึกษาคือการเข้าถึงขัอมูลตามดัชนี</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e89feac8",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_series = pd.Series([10, 20, 30, 40, 50], index=['num1', 'num2', 'num3', 'num4', 'num5'])\n",
    "print(my_series[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62fe09e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(my_series['num3'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e2fe4e0",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">สามารถใช้ฟังก์ชัน <font face=\"Monaco\">min()</font> <font face=\"Monaco\">max()</font> และ <font face=\"Monaco\">mean()</font> จากโมดูล NumPy เพื่อค้นหาค่าต่ำสุดและค่าสูงสุดจาก Series ได้ตามลำดับ</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3469b43",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_series = pd.Series([5, 8, 2, 11, 9])\n",
    "print(np.min(my_series))\n",
    "print(np.max(my_series))\n",
    "print(np.mean(my_series))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4822eb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(np.median(my_series))\n",
    "print(my_series.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "945ce1d8",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">การแปลง Series เป็นข้อมูลประเภทอื่น ๆ</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38521253",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_series.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "126c17c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_series.to_dict()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28bae57a",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">Pandas Dataframe</h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">Pandas DataFrame คือโครงสร้างข้อมูลแบบตารางที่จัดเก็บข้อมูลในรูปแบบของ แถว (Rows) และ คอลัมน์ (Columns) . ตามมาตรฐานแล้ว แถวจะสอดคล้องกับระเบียนข้อมูล (Records) ขณะที่คอลัมน์จะหมายถึงคุณลักษณะ (Attributes) พูดง่าย ๆ ก็คือ Pandas DataFrame เป็นการรวมกันของ Series หลาย ๆ Series</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "817d1d00",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df = pd.DataFrame()\n",
    "print(my_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67afaf6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [['Mathematics', 85], ['English', 91], ['History', 95]]\n",
    "my_df = pd.DataFrame(scores, columns = ['Subject','Score'])\n",
    "my_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea1a5960",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = {'Subject':[\"Mathematics\", \"History\", \"English\", \"Science\", \"Arts\"], 'Score':[98, 75, 68, 82, 99]}\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81ab10cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85},\n",
    "          {'Subject':'History', 'Score':98},\n",
    "          {'Subject':'English', 'Score':76},\n",
    "          {'Subject':'Science', 'Score':72},\n",
    "          {'Subject':'Arts', 'Score':95},]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06d20c3a",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">Dictionaries ที่ถูกนำมาใช้ภายในลิสต์เพื่อสร้าง Pandas DataFrame นั้น ไม่จำเป็นต้องมีขนาดเท่ากัน</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fdb8e5be",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85},\n",
    "          {'Subject':'History', 'Score':98},\n",
    "          {'Subject':'English', 'Score':76},\n",
    "          {'Score':72},\n",
    "          {'Subject':'Arts', 'Score':95},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "624df462",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เราสามารถเรียกใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">head</span> เพื่อดูข้อมูลในแถวบนสุด ของ DataFrame ได้ ดังที่แสดงในตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8dbf6c5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85},\n",
    "          {'Subject':'History', 'Score':98},\n",
    "          {'Subject':'English', 'Score':76},\n",
    "          {'Subject':'Science', 'Score':72},\n",
    "          {'Subject':'Arts', 'Score':95},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da0d2740",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เราสามารถเรียกใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">tail</span> เพื่อดูข้อมูลในแถวล่างสุด ของ DataFrame ได้ ดังที่แสดงในตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a161cba5",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.tail(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "363e2055",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในผลลัพธ์ด้านล่าง เราสามารถแสดง จำนวนรายการ (Entries) ใน Pandas DataFrame, จำนวนคอลัมน์ (Columns) พร้อมกับประเภทข้อมูลของคอลัมน์ (Column type) และรายละเอียดอื่น ๆ อีก</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7660eb62",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eee8d196",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b8639ca",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">Importing Data in Pandas</h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">เราสามารถนำเข้าข้อมูลจากแหล่งอื่นไปยัง Pandas DataFrame ได้ดังตัวอย่างต่อไปนี้</p>\n",
    "\n",
    "<h2 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">Importing CSV</h2>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c69724c4",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เราสามารถเรียกใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">read_csv</span> เพื่อนำเข้าข้อมูลประเภท csv ไปยัง DataFrame ได้ ดังที่แสดงในตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61bf1d9a",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('username.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "723fd974",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในบางครั้งไฟล์ csv ที่เราได้มาไม่ได้มีหัวตารางมาให้ เราสามารถเพิ่ม หรือแก้ไขได้เองได้ดังตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8aee925",
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = ['Username', 'Identifier', 'First Name', 'Last Name']\n",
    "data = pd.read_csv('username.csv', header=0, names=headers)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70658cbc",
   "metadata": {},
   "source": [
    "<h2 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">Importing TSV</h2>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">ไฟล์ประเภท tsv คล้ายคลึงกับ csv แต่จะแยกคอลัมน์ด้วยในใช้ tab ซึ่งเราก็สามารถนำเข้าได้ เช่นกันดังที่แสดงในตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8216c1f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('username.tsv', sep='\\t')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d1703fc",
   "metadata": {},
   "source": [
    "# Exercise1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4af0b9c4",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การจัดการกับข้อมูลที่ขาดหายไปโดย Pandas</h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">ค่าที่ขาดหายไป (Missing Values) คือข้อมูลสังเกตการณ์ (Observations) ในชุดข้อมูลที่ไม่มีค่าใด ๆ อยู่เลย  Pandas สามารถเปลี่ยนแปลงรูปแบบของข้อมูล (Data Patterns) ของค่าที่ขาดหายไปได้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84c851f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "titanic_data = sns.load_dataset('titanic')\n",
    "titanic_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f8da2749",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในขั้นตอนต่อไปเราจะทำการแสดงเฉพาะข้อมูลที่เกี่ยวข้อง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb7ec350",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced = titanic_data[[\"survived\",\"pclass\",\"age\",\"fare\"]]\n",
    "titanic_data_reduced.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17fb7976",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในการหาข้อมูลที่ขาดหายไปในคอลัมน์ เราเรียกกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">isnull</span> และใช้ <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">mean</span> ดังแสดงในตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e67df1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced.isnull().mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aff4b5fc",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">จากผลลัพธ์เราจะเห็นได้ว่าคอลัมน์ age มีข้อมูลที่หายไปถึง 19.8% เราสามารถหาค่า median และ mean ของคอลัมน์ age ได้ ดังนี้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f5f4d48",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "median = titanic_data_reduced.age.median()\n",
    "print(median)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "962bb54e",
   "metadata": {},
   "outputs": [],
   "source": [
    "mean = titanic_data_reduced.age.mean()\n",
    "print(mean)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bed754da",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในขั้นตอนต่อไปเราจะสร้างคอลัมน์ใหม่ขึ้นมา แล้วทำการแทนข้อมูลที่หายไปด้วยค่า median และ mean</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "431e64df",
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "titanic_data_filled = copy.deepcopy(titanic_data_reduced)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9124510",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_filled['Median_Age'] = titanic_data_reduced.age.fillna(median)\n",
    "titanic_data_filled['Mean_Age'] = titanic_data_reduced.age.fillna(mean)\n",
    "titanic_data_filled['Mean_Age'] = np.round(titanic_data_filled['Mean_Age'], 1)\n",
    "titanic_data_filled.head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc4a4181",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">การเติมค่าเฉลี่ย (Mean Imputation) และค่ามัธยฐาน (Median Imputation) อาจส่งผลกระทบต่อ การกระจายตัวของข้อมูล (Data Distribution) สำหรับคอลัมน์ที่มีค่าที่ขาดหายไป</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">โดยเฉพาะอย่างยิ่ง ความแปรปรวน (Variance) ของคอลัมน์จะลดลงจากการเติมด้วยค่าเฉลี่ยและมัธยฐาน เนื่องจากมีการเพิ่มค่าเข้าไปที่จุดศูนย์กลางของการกระจายตัวมากขึ้น</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16204113",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "\n",
    "titanic_data_filled['age'].plot(kind='kde', ax=ax)\n",
    "\n",
    "titanic_data_filled['Median_Age'].plot(kind='kde', ax=ax, color='red')\n",
    "\n",
    "titanic_data_filled['Mean_Age'].plot(kind='kde', ax=ax, color='green')\n",
    "\n",
    "lines, labels = ax.get_legend_handles_labels()\n",
    "ax.legend(lines, labels, loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88876dae",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">การเติมข้อมูลที่หายไปด้วยค่าเฉลี่ย (Mean Imputation) และค่ามัธยฐาน (Median Imputation) สามารถนำมาใช้กับข้อมูลตัวเลขที่ขาดหายไปได้ ในกรณีที่ข้อมูลนั้น ขาดหายไปแบบสุ่ม (Missing at Random)</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">หากข้อมูลมีการ แจกแจงแบบปกติ (Normally Distributed) จะนิยมใช้ การเติมด้วยค่าเฉลี่ย (Mean Imputation)</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">ในทางกลับกัน หากข้อมูลมีการ แจกแจงแบบเบ้ (Skewed Distributions) จะนิยมใช้ การเติมด้วยค่ามัธยฐาน (Median Imputation) มากกว่า</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25e777e2",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การจัดการกับค่าที่ขาดหายไปในข้อมูลเชิงหมวดหมู่ (Categorical Values)</h1>\n",
    "\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">วิธีที่พบบ่อยที่สุดวิธีหนึ่งในการจัดการกับค่าที่ขาดหายไปในคอลัมน์เชิงหมวดหมู่ คือการ แทนที่ค่าที่ขาดหายไปด้วยค่าที่เกิดขึ้นบ่อยที่สุด (Most Frequently Occurring Values) ซึ่งก็คือ ฐานนิยม (Mode) ของคอลัมน์นั่นเอง ด้วยเหตุนี้การเติมค่าด้วยหมวดหมู่ที่พบบ่อย (Frequent Category Imputation) จึงเป็นที่รู้จักกันในชื่อ การเติมด้วยฐานนิยม (Mode Imputation) ดังที่แสดงให้ตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a969112",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "titanic_data = sns.load_dataset('titanic')\n",
    "titanic_data_reduced = titanic_data[['embark_town', 'age', 'fare']]\n",
    "titanic_data_reduced.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39b8f094",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced.isnull().mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d0db109",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ผลลัพธ์แสดงให้เห็นว่าคอลัมน์ embark_town และคอลัมน์ age มีค่าที่ขาดหายไป (Missing Values) อัตราส่วนของค่าที่ขาดหายไปสำหรับคอลัมน์ embark_town นั้นมีน้อยมาก ต่อไปเราจะทำการพลอตข้อมูลเชิงหมวดหมู่</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "368065c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced.embark_town.value_counts().sort_values(ascending=False).plot.bar(\n",
    "    title='Passenger Counts by Embarkation Town',\n",
    "    xlabel='Embarkation Town',\n",
    "    ylabel='Number of Passengers'\n",
    ")\n",
    "plt.xlabel('Embark Town')\n",
    "plt.ylabel('Number of Passengers');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b2a76e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced.embark_town.mode()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12603f70",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ต่อไปเราจะแทนที่ค่าที่ขาดหายไปในคอลัมน์ embark_town ด้วยค่า Southampton</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc45f071",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced.embark_town.fillna('Southampton', inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de8bc1f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced.age.mode()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0001075f",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ผลลัพธ์แสดงให้เห็นว่า ฐานนิยม (Mode) ของคอลัมน์ age คือ 24 ดังนั้น เราจึงสามารถใช้ค่านี้เพื่อแทนที่ค่าที่ขาดหายไปในคอลัมน์ age</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6b7ed5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data_reduced['age_mode'] = titanic_data_reduced.age.fillna(24)\n",
    "\n",
    "titanic_data_reduced.head(20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a67aed99",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เราจะพลอตการกระจายตัวของข้อมูล age และ age_mode เพื่อเปรียบเทียบ</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc30f5c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot(111)\n",
    "\n",
    "titanic_data_reduced['age'].plot(kind='kde', ax=ax)\n",
    "titanic_data_reduced['age_mode'].plot(kind='kde', ax=ax, color='red')\n",
    "\n",
    "lines, labels = ax.get_legend_handles_labels()\n",
    "ax.legend(lines, labels, loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2d626c7",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การจัดการ Pandas DataFrames</h1>\n",
    "    \n",
    "<p style=\"font-size: 16px; line-height:2.0;\">เมื่อเราทำการโหลดข้อมูลเข้าสู่ Pandas DataFrame แล้ว เราอาจมีความจำเป็นต้อง จัดการข้อมูล (Manipulate the Data) เพิ่มเติมแเช่น</p>\n",
    "\n",
    "<ul>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">การกรองคอลัมน์ที่ต้องการ (Filtering certain columns)</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">การละทิ้งคอลัมน์อื่น ๆ (Dropping the others)</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">การเลือกชุดย่อยของแถวหรือคอลัมน์ (Selecting a subset of rows or columns)</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">การเรียงลำดับข้อมูล (Sorting the data)</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">การค้นหาค่าที่ไม่ซ้ำกัน (Finding unique values) และอื่น ๆ</div></li>\n",
    " \n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbcadb16",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h2 style=\"color:#011993;font-family:'Kanit'; font-size:20px\">การเลือกข้อมูลโดยใช้ การกำหนดดัชนี (Indexing) และ การแบ่งส่วนข้อมูล (Slicing)</h2>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">การกำหนดดัชนี (Indexing) หมายถึงการดึงข้อมูลโดยใช้ข้อมูลดัชนี (Index) หรือคอลัมน์ (Column) ของ Pandas DataFrame ซึ่งเราสามารถศึกษาได้จากตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cca42c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "titanic_data = sns.load_dataset('titanic')\n",
    "titanic_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bba0002e",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h2 style=\"color:#011993;font-family:'Kanit'; font-size:20px\">การเลือกข้อมูลโดยใช้วงเล็บปีกกา []</h2>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">วิธีที่ง่ายที่สุดวิธีหนึ่งในการเลือกข้อมูลจากคอลัมน์ต่าง ๆ คือการใช้ วงเล็บปีกกา [] หากต้องการดึงข้อมูลคอลัมน์ในรูปแบบของ ซีรีส์ (Series) ออกจาก Pandas DataFrame คุณจะต้องระบุ ชื่อคอลัมน์ ไว้ภายในวงเล็บปีกกาที่ตามหลังชื่อ Pandas DataFrame นั้น</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e299bca",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(titanic_data[\"class\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79a1e89c",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(titanic_data[\"class\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c58ec270",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เราสามารถเลือกหลายคอลัมน์ได้โดยการใช้ลิสต์ของชื่อคอลัมน์ (List of Column Names) ที่อยู่ในวงเล็บปีกกา [] ดังที่แสดงไว้ด้านล่างนี้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "493137cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "titanic_data[[\"class\", \"sex\", \"age\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5213a490",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df = titanic_data[titanic_data[\"sex\"] == \"male\"]\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "261c8e9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df = titanic_data[(titanic_data[\"sex\"] == \"male\") & (titanic_data[\"class\"] == \"First\")]\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec2ae7c8",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถใช้ฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">isin()</span> เพื่อระบุ ช่วงของค่า (Range of Values) สำหรับการกรองระเบียน (Records) ได้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c658cae9",
   "metadata": {},
   "outputs": [],
   "source": [
    "ages = [20,21,22]\n",
    "age_dataset = titanic_data[titanic_data[\"age\"].isin(ages)]\n",
    "age_dataset.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3f89816",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h2 style=\"color:#011993;font-family:'Kanit'; font-size:20px\">การกำหนดดัชนี (Indexing) และการแบ่งส่วนข้อมูล (Slicing) โดยใช้ฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:20px;\">loc</span></h2>\n",
    "\n",
    "<p>เราสามารถใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">loc</span> จาก Pandas DataFrame ยังสามารถใช้เพื่อ กรองระเบียน (Filter Records) ใน Pandas DataFrame ได้ดังตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf82076c",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85, 'Grade': 'B', 'Remarks': 'Good',},\n",
    "          {'Subject':'History', 'Score':98, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "          {'Subject':'English', 'Score':76, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Science', 'Score':72, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Arts', 'Score':95, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1c9e86c",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(my_df.loc[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99a7d824",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(my_df.loc[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e980856b",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ed0d64e",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[2:4, [\"Grade\", \"Score\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f53cdc8",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">\n",
    "นอกเหนือจากการส่งดัชนีที่เป็นตัวเลขตามปกติแล้วเรายังสามารถส่ง ดัชนีแบบตั้งชื่อไปยังฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">loc</span> ได้ด้วย   \n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad24edaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85, 'Grade': 'B', 'Remarks': 'Good',},\n",
    "          {'Subject':'History', 'Score':98, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "          {'Subject':'English', 'Score':76, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Science', 'Score':72, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Arts', 'Score':95, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores, index = [\"Student1\", \"Student2\", \"Student3\", \"Student4\", \"Student5\"])\n",
    "my_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d51fd61",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[\"Student1\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f834a949",
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = [\"Student1\", \"Student2\"]\n",
    "my_df.loc[index_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c77d7ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[\"Student1\", \"Grade\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68ae611d",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในกรณีเดียวกันกับการแบ่งส่วนข้อมูลตัวเลข เราสามารถทำการแบ่งส่วนข้อมูลที่เป็นช่วงของระเบียนได้ ดังตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7780cfbe",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "my_df.loc[\"Student1\":\"Student2\", \"Grade\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7dcb9b07",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[\"Student1\":\"Student4\", \"Grade\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0e7f3d74",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถระบุลิสต์ของค่าบูลีน (Boolean Values) ที่สอดคล้องกับดัชนีที่คุณต้องการเลือกโดยใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">loc</span> ได้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c29b9da4",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[[False, False, False, True, False]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c8ced58",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในตัวอย่างต่อไปจะเป็นการใช้นิพจน์บูลีน ในการกรองระเบียน</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77b39da9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "my_df[\"Score\"] > 80"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f55dbdcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[my_df[\"Score\"] > 80]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "705d0523",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[(my_df[\"Score\"]>80) & (my_df[\"Remarks\"] == \"Excellent\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ed65a61",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.loc[my_df[\"Score\"]>80, [\"Score\", \"Grade\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bea08b3b",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h2 style=\"color:#011993;font-family:'Kanit'; font-size:20px\">การกำหนดดัชนี (Indexing) และการแบ่งส่วนข้อมูล (Slicing) โดยใช้ฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:20px;\">iloc</span></h2>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถใช้ฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">iloc</span> สำหรับการเลือกและการแบ่งส่วนระเบียน (selecting and slicing records) โดยใช้ค่าดัชนี (index values) ได้อีกด้วย</p>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">\n",
    "    อย่างไรก็ตาม ข้อแตกต่างจากฟังก์ชัน loc คือ\n",
    "</p>\n",
    "\n",
    "<ul>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">ฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">loc</span> สามารถส่งผ่านได้ทั้งดัชนีที่เป็นสตริง (string indexes) และดัชนีที่เป็นตัวเลข (integer indexes)</div></li>\n",
    "    <li><div style=\"font-size: 16px; line-height:2.0;\">ฟังก์ชัน <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">iloc</span> สามารถส่งผ่านได้เฉพาะค่าดัชนีที่เป็นตัวเลขเท่านั้น</div></li> \n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8bc6e98",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85, 'Grade': 'B', 'Remarks': 'Good',},\n",
    "          {'Subject':'History', 'Score':98, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "          {'Subject':'English', 'Score':76, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Science', 'Score':72, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Arts', 'Score':95, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6d35ffcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iloc[3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83042fb0",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">หากเราต้องการให้แสดงผลลัพธ์ออกมาในรูปแบบ DataFrame เราต้องระบุค่าดัชนี (index) ไว้ในวงเล็บปีกกา [ ]</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f4b4613",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iloc[[3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36c7b47d",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iloc[[2,3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cea2851",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iloc[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "37553520",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iloc[[2,3], [0,1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71ea4d82",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.iloc[2:4, 0:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56fe5ef8",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การลบแถว (Rows) และคอลัมน์ (Columns) ด้วยกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:30px;\">drop</span></h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">นอกเหนือจากการเลือกคอลัมน์โดยใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">loc</span> และ <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">iloc</span> แล้ว เรายังสามารถใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">drop()</span> เพื่อลบแถวและคอลัมน์ที่ไม่ต้องการ ออกจาก DataFrame ในขณะที่ยังคงเหลือแถวและคอลัมน์อื่น ๆ ไว้ได้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2521fdf9",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85, 'Grade': 'B', 'Remarks': 'Good',},\n",
    "          {'Subject':'History', 'Score':98, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "          {'Subject':'English', 'Score':76, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Science', 'Score':72, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Arts', 'Score':95, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a653275",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2 = my_df.drop([1,4])\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2b40adc",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เราสามารถรีเซ็ตดัชนีของ DataFrame ให้เริ่มต้นจาก 0 โดยใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">reset_index</span> ได้ดังตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0446eda",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2.reset_index(inplace=True)\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0bde5ef",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">จากผลลัพธ์ที่แสดงข้างต้น เราจะเห็นได้ว่าดัชนีได้ถูกรีเซ็ตแล้ว นอกจากนี้เราจะเห็นว่ามีการเพิ่มคอลัมน์ใหม่ชื่อ index เข้ามาซึ่งบรรจุค่าดัชนีเดิมไว้ หากเราต้องการเพียงแค่รีเซ็ตดัชนีใหม่โดยไม่สร้างคอลัมน์ใหม่ที่ชื่อ index สามารถทำได้โดยการส่งค่า <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">True</span> ให้กับพารามิเตอร์ <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">drop</span> ของกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">reset_index</span></p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecba1ce9",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2 = my_df.drop([1,4])\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63272194",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2.reset_index(inplace=True, drop = True)\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "747c80d4",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">โดยค่าเริ่มต้นแล้วกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">drop</span> จะไม่ลบแถวในตำแหน่งเดิม (in place)</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41fe749e",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.drop([1,3,4])\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d920cb2",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ถ้ากราต้องการลบแถวในตำแหน่งเดิม (in place) เราต้องส่งค่า <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">True</span> ให้กับแอตทริบิวต์ inplace ดังที่แสดงในตัวอย่างด้านล่างนี้</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed447f55",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.drop([1,3,4], inplace = True)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e5ce5f7",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">\n",
    "ในทำนองเดียวกันเราสามารถลบคอลัมน์โดยใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">drop</span> ได้ตามตัวอย่าง\n",
    "</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9c9c269",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85, 'Grade': 'B', 'Remarks': 'Good',},\n",
    "          {'Subject':'History', 'Score':98, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "          {'Subject':'English', 'Score':76, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Science', 'Score':72, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Arts', 'Score':95, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88939c84",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2 = my_df.drop([\"Subject\", \"Grade\"], axis = 1)\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67d1c091",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df.drop([\"Subject\", \"Grade\"], axis = 1, inplace=True)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a76ffec2",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การกรองแถวและคอลัมน์ (Columns) ด้วยกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:30px;\">filter</span></h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">drop</span> จะทำการลบระเบียนที่ไม่ต้องการออกไปส่วนdrop <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">filter</span> จะทำงานในทางกลับกันคือจะเก็บระเบียนที่ต้องการไว้จากชุดระเบียนใน Pandas DataFrame</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b73d938",
   "metadata": {},
   "outputs": [],
   "source": [
    "scores = [{'Subject':'Mathematics', 'Score':85, 'Grade': 'B', 'Remarks': 'Good',},\n",
    "          {'Subject':'History', 'Score':98, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "          {'Subject':'English', 'Score':76, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Science', 'Score':72, 'Grade': 'C', 'Remarks':'Fair'},\n",
    "          {'Subject':'Arts', 'Score':95, 'Grade': 'A', 'Remarks':'Excellent'},\n",
    "         ]\n",
    "my_df = pd.DataFrame(scores)\n",
    "my_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c43d6370",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2 = my_df.filter([1,3,4], axis = 0)\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c649d3f3",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถรีเซ็ตดัชนีหลังจากที่ทำการกรองข้อมูลแล้ว โดยใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">reset_index</span> ดังที่แสดงในตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d37c61db",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2 = my_df2.reset_index(drop=True)\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2230a90a",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถกรองคอลัมน์ได้ เช่นเดียวกัน</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8107821e",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_df2 = my_df.filter([\"Score\", \"Grade\"], axis = 1)\n",
    "my_df2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1c1779e",
   "metadata": {},
   "source": [
    "<link href=\"https://fonts.googleapis.com/css?family=Athiti|Bai+Jamjuree|Chakra+Petch|Kanit|Open+Sans|Prompt|Sarabun\" rel=\"stylesheet\">\n",
    "\n",
    "<h1 style=\"color:#011993;font-family:'Kanit'; font-size:30px\">การเรียงลำดับ DataFrame</h1>\n",
    "\n",
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถเรียงลำดับระเบียนใน Pandas DataFrame ได้ตามตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f936217c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "titanic_data = sns.load_dataset('titanic')\n",
    "titanic_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4bc4874",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">ในการเรียงลำดับ DataFrame เราสามารถใช้กระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">sort_values</span> ได้\n",
    "\n",
    "ลิสต์ของคอลัมน์ที่ใช้ในการเรียงลำดับจะต้องถูกส่งผ่านไปยังแอตทริบิวต์ <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">by</span> ของกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">sort_values</span></p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5371485",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_sorted_data = titanic_data.sort_values(by=['age'])\n",
    "age_sorted_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f7b1807",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">กรณีที่เรียงลำดับจากมากไปน้อย</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "050c694a",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_sorted_data = titanic_data.sort_values(by=['age'], ascending = False)\n",
    "age_sorted_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5741406d",
   "metadata": {},
   "source": [
    "<p style=\"font-size: 16px; line-height:2.0;\">เรายังสามารถเรียงลำดับหลายคอลัมน์ในกระบวนวิธี <span face=\"Monaco\" style=\"font-family: Monaco; font-size:15px;\">sort_values</span> ได้ตามตัวอย่าง</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67047873",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "age_sorted_data = titanic_data.sort_values(by=['age', 'fare'], ascending = False)\n",
    "age_sorted_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e3679e4",
   "metadata": {},
   "source": [
    "# Exercise2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
