{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "aca7f56a",
   "metadata": {},
   "source": [
    "# 02 반도체 공정 데이터 분석 · 2. 데이터 탐색\n",
    "\n",
    "- 강의 페이지: `Web/강좌/02_반도체_공정_데이터분석/반도체_공정_데이터분석.html` → 목차 **데이터 탐색**\n",
    "- CSV를 DataFrame으로 읽고 크기·통계·행/열 선택·값 세기를 연습합니다.\n",
    "- `반도체_공정_샘플.csv`가 이 노트북과 같은 폴더에 있어야 합니다.\n",
    "- 위에서 아래로 순서대로 실행하세요. 다른 노트북의 변수를 사용하지 않으므로 이 파일만 열어도 실행됩니다."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c71defea",
   "metadata": {},
   "source": [
    "### 1 CSV 파일 읽어서 DataFrame 만들기\n",
    "- 설명: CSV 파일을 pandas DataFrame으로 읽어 분석 준비를 합니다."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ab93486",
   "metadata": {},
   "source": [
    "### 준비 · 라이브러리 불러오기 (1단계와 동일)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c8db40d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 📦 분석에 사용할 라이브러리 불러오기\n",
    "# 'as pd'는 앞으로 pandas를 pd라는 짧은 이름으로 부르겠다는 약속\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 그래프 한글 설정 (Mac은 'AppleGothic')\n",
    "plt.rcParams['font.family'] = 'Malgun Gothic'\n",
    "plt.rcParams['axes.unicode_minus'] = False   # 음수(-) 기호 깨짐 방지\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "649b0da1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 📂 CSV 파일을 읽어서 df라는 이름의 DataFrame으로 저장\n",
    "df = pd.read_csv('반도체_공정_샘플.csv')\n",
    "\n",
    "# 데이터 크기 확인: (행의 수, 열의 수)\n",
    "print(f\"데이터 크기: {df.shape[0]}행 × {df.shape[1]}열\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e71dd6b5",
   "metadata": {},
   "source": [
    "### 2 데이터를 처음 받으면 꼭 해보는 5가지\n",
    "- 설명: head, info, describe 등으로 데이터의 첫인상과 구조를 확인합니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d8ea605",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ① 처음 5줄 미리보기\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5622ec6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ② 데이터 크기\n",
    "print(\"크기:\", df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4130697c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ③ 전체 구조 요약 (열 이름·타입·결측 수)\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3454f170",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ④ 숫자 열의 기술 통계\n",
    "df.describe().round(2)   # 보기 좋게 소수점 2자리 반올림"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1288c0ec",
   "metadata": {},
   "source": [
    "### 3 원하는 \"열(컬럼)\"만 골라 보기\n",
    "- 설명: 필요한 행과 열만 골라 보며 DataFrame 선택 문법을 연습합니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "000dc5a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 한 개 열만 선택 → 결과는 Series (열 하나짜리)\n",
    "df['온도_섭씨']            # 온도_섭씨 한 줄만 200개"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f04ce78c",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 여러 열 선택 → 결과는 DataFrame (대괄호 두 겹!)\n",
    "df[['온도_섭씨', '압력_Pa', '합격여부']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "468d709b",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 한 개 열의 통계 계산\n",
    "print(f\"평균 온도: {df['온도_섭씨'].mean():.2f}°C\")\n",
    "print(f\"최고 온도: {df['온도_섭씨'].max():.2f}°C\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "239e37e3",
   "metadata": {},
   "source": [
    "### 4 원하는 \"행\"만 골라 보기 — loc vs iloc\n",
    "- 설명: loc/iloc을 사용해 라벨 기준과 위치 기준 선택의 차이를 확인합니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aab2c212",
   "metadata": {},
   "outputs": [],
   "source": [
    "## iloc - 위치(번호)로 선택. 0부터 시작!\n",
    "df.iloc[0]          # 첫 번째 행 (가장 위 한 줄)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd65eed8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.iloc[1:5]       # 1~4번 행 (5번 미포함!)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28633dc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "## loc - 인덱스 이름(라벨)으로 선택\n",
    "df.loc[0]              # 인덱스가 0인 행"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83ffb1ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.loc[0:4, ['온도_섭씨', '합격여부']]  # 행+열 동시 지정"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f91050e6",
   "metadata": {},
   "source": [
    "### 5 카테고리 값 세기 — value_counts()\n",
    "- 설명: 범주형 값이 몇 번씩 등장하는지 세어 데이터 분포를 파악합니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93b5f019",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 합격여부 컬럼의 값별 개수\n",
    "print(df['합격여부'].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ce521c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 비율(%)로 보고 싶다면 normalize=True\n",
    "print(df['합격여부'].value_counts(normalize=True) * 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e46a844",
   "metadata": {},
   "source": [
    "## 마무리\n",
    "\n",
    "- `read_csv()`로 읽고 `head()`, `info()`, `describe()`로 첫인상을 확인했습니다.\n",
    "- `df['열']`, `df[['열1', '열2']]`, `loc`/`iloc`으로 원하는 부분을 골랐습니다.\n",
    "- `value_counts()`로 합격 180건(90%) vs 불합격 20건(10%)의 **불균형**을 확인했습니다."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
