File size: 1,147 Bytes
f6086a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ecafd98",
   "metadata": {},
   "source": [
    "# Assignment 3: Feature Engineering - Encoding Categorical Data\n",
    "Create a function to encode categorical variables in a dataset using one-hot encoding.\n",
    "Dataset contains student info with grades (A, B, C) and gender (M, F)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44ac96ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "    \n",
    "    # Synthetic dataset\n",
    "data = {\n",
    "        'student': ['Alice', 'Bob', 'Charlie'],\n",
    "        'grade': ['A', 'B', 'C'],\n",
    "        'gender': ['F', 'M', 'F']\n",
    "    }\n",
    "df = pd.DataFrame(data)\n",
    "\n",
    "    # One-hot encoding\\n\",\n",
    "df_encoded = pd.get_dummies(df, columns=['grade', 'gender']) \n",
    "print(df_encoded)\n",
    "    \n",
    "    # Error: Trying to access non-existent column\\n\",\n",
    "print(df_encoded['grade_D'])"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}