{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#importing the basic libraries\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#read the CSV file\n",
    "agedf = pd.read_csv(\"Age.Manipulation.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agedf.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#we see a few missings.. need to organize\n",
    "agedf.race.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#we need to check how much % missing values are there (?)\n",
    "print(agedf.shape)\n",
    "print((2273/101766)*100)\n",
    "\n",
    "#so less than 5%; we can replace with mode\n",
    "agedf.race = agedf.race.replace(to_replace='?', value = np.nan)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agedf.race.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agedf.race.fillna('Caucasian',inplace=True)\n",
    "agedf.race.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Dealing with the data entry errors (we can simply replace with Mode as they are not many)\n",
    "agedf.race = agedf.race.replace(to_replace='Middle Class', value='Caucasian')\n",
    "agedf.race = agedf.race.replace(to_replace='24/04/2018', value='Caucasian')\n",
    "agedf.race = agedf.race.replace(to_replace='male', value='Caucasian')\n",
    "agedf.race = agedf.race.replace(to_replace='female', value='Caucasian')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#what about age\n",
    "agedf.race.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#one missing value\n",
    "agedf.age.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#age contains one data entry error, which we can replace with mode\n",
    "agedf.age = agedf.age.replace(to_replace='24000', value='[70-80)')\n",
    "agedf.age.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#we can replace with [70-80)\n",
    "agedf.age.isna().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agedf.age.fillna('[70-80)',inplace=True)\n",
    "agedf.age.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now, we need to do something about the age, i.e., replace with average of range\n",
    "agedf.age = agedf.age.str.replace('[',\"\")\n",
    "agedf.age = agedf.age.str.replace(')',\"\")\n",
    "agedf.age\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "age_beg = agedf.age.str.split('-').apply(lambda x: x[0])\n",
    "age_end = agedf.age.str.split('-').apply(lambda x: x[1])\n",
    "\n",
    "#cast both as integers\n",
    "age_beg = age_beg.astype(int)\n",
    "age_end = age_end.astype(int)\n",
    "\n",
    "#create a separate column\n",
    "agedf['Modified'] = (age_beg + age_end)/2\n",
    "agedf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "agedf.drop('age',axis=1)"
   ]
  }
 ],
 "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
