{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c7ee241f-c0dd-4e15-b196-da2d12ce0601",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2184cef4-5ad2-41e2-912a-bdc9ceb08280",
   "metadata": {},
   "outputs": [],
   "source": [
    "df=pd.read_csv(\"C:\\\\Users\\\\daroo\\\\Downloads\\\\Global YouTube Statistics.csv\",encoding=\"latin1\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c0397479-55fb-45f5-aee3-958bdcc7f538",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original Shape: (995, 28)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>rank</th>\n",
       "      <th>Youtuber</th>\n",
       "      <th>subscribers</th>\n",
       "      <th>video views</th>\n",
       "      <th>category</th>\n",
       "      <th>Title</th>\n",
       "      <th>uploads</th>\n",
       "      <th>Country</th>\n",
       "      <th>Abbreviation</th>\n",
       "      <th>channel_type</th>\n",
       "      <th>...</th>\n",
       "      <th>subscribers_for_last_30_days</th>\n",
       "      <th>created_year</th>\n",
       "      <th>created_month</th>\n",
       "      <th>created_date</th>\n",
       "      <th>Gross tertiary education enrollment (%)</th>\n",
       "      <th>Population</th>\n",
       "      <th>Unemployment rate</th>\n",
       "      <th>Urban_population</th>\n",
       "      <th>Latitude</th>\n",
       "      <th>Longitude</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>T-Series</td>\n",
       "      <td>245000000</td>\n",
       "      <td>2.280000e+11</td>\n",
       "      <td>Music</td>\n",
       "      <td>T-Series</td>\n",
       "      <td>20082</td>\n",
       "      <td>India</td>\n",
       "      <td>IN</td>\n",
       "      <td>Music</td>\n",
       "      <td>...</td>\n",
       "      <td>2000000.0</td>\n",
       "      <td>2006.0</td>\n",
       "      <td>Mar</td>\n",
       "      <td>13.0</td>\n",
       "      <td>28.1</td>\n",
       "      <td>1.366418e+09</td>\n",
       "      <td>5.36</td>\n",
       "      <td>471031528.0</td>\n",
       "      <td>20.593684</td>\n",
       "      <td>78.962880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>YouTube Movies</td>\n",
       "      <td>170000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>Film &amp; Animation</td>\n",
       "      <td>youtubemovies</td>\n",
       "      <td>1</td>\n",
       "      <td>United States</td>\n",
       "      <td>US</td>\n",
       "      <td>Games</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2006.0</td>\n",
       "      <td>Mar</td>\n",
       "      <td>5.0</td>\n",
       "      <td>88.2</td>\n",
       "      <td>3.282395e+08</td>\n",
       "      <td>14.70</td>\n",
       "      <td>270663028.0</td>\n",
       "      <td>37.090240</td>\n",
       "      <td>-95.712891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>MrBeast</td>\n",
       "      <td>166000000</td>\n",
       "      <td>2.836884e+10</td>\n",
       "      <td>Entertainment</td>\n",
       "      <td>MrBeast</td>\n",
       "      <td>741</td>\n",
       "      <td>United States</td>\n",
       "      <td>US</td>\n",
       "      <td>Entertainment</td>\n",
       "      <td>...</td>\n",
       "      <td>8000000.0</td>\n",
       "      <td>2012.0</td>\n",
       "      <td>Feb</td>\n",
       "      <td>20.0</td>\n",
       "      <td>88.2</td>\n",
       "      <td>3.282395e+08</td>\n",
       "      <td>14.70</td>\n",
       "      <td>270663028.0</td>\n",
       "      <td>37.090240</td>\n",
       "      <td>-95.712891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Cocomelon - Nursery Rhymes</td>\n",
       "      <td>162000000</td>\n",
       "      <td>1.640000e+11</td>\n",
       "      <td>Education</td>\n",
       "      <td>Cocomelon - Nursery Rhymes</td>\n",
       "      <td>966</td>\n",
       "      <td>United States</td>\n",
       "      <td>US</td>\n",
       "      <td>Education</td>\n",
       "      <td>...</td>\n",
       "      <td>1000000.0</td>\n",
       "      <td>2006.0</td>\n",
       "      <td>Sep</td>\n",
       "      <td>1.0</td>\n",
       "      <td>88.2</td>\n",
       "      <td>3.282395e+08</td>\n",
       "      <td>14.70</td>\n",
       "      <td>270663028.0</td>\n",
       "      <td>37.090240</td>\n",
       "      <td>-95.712891</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>SET India</td>\n",
       "      <td>159000000</td>\n",
       "      <td>1.480000e+11</td>\n",
       "      <td>Shows</td>\n",
       "      <td>SET India</td>\n",
       "      <td>116536</td>\n",
       "      <td>India</td>\n",
       "      <td>IN</td>\n",
       "      <td>Entertainment</td>\n",
       "      <td>...</td>\n",
       "      <td>1000000.0</td>\n",
       "      <td>2006.0</td>\n",
       "      <td>Sep</td>\n",
       "      <td>20.0</td>\n",
       "      <td>28.1</td>\n",
       "      <td>1.366418e+09</td>\n",
       "      <td>5.36</td>\n",
       "      <td>471031528.0</td>\n",
       "      <td>20.593684</td>\n",
       "      <td>78.962880</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 28 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   rank                    Youtuber  subscribers   video views  \\\n",
       "0     1                    T-Series    245000000  2.280000e+11   \n",
       "1     2              YouTube Movies    170000000  0.000000e+00   \n",
       "2     3                     MrBeast    166000000  2.836884e+10   \n",
       "3     4  Cocomelon - Nursery Rhymes    162000000  1.640000e+11   \n",
       "4     5                   SET India    159000000  1.480000e+11   \n",
       "\n",
       "           category                       Title  uploads        Country  \\\n",
       "0             Music                    T-Series    20082          India   \n",
       "1  Film & Animation               youtubemovies        1  United States   \n",
       "2     Entertainment                     MrBeast      741  United States   \n",
       "3         Education  Cocomelon - Nursery Rhymes      966  United States   \n",
       "4             Shows                   SET India   116536          India   \n",
       "\n",
       "  Abbreviation   channel_type  ...  subscribers_for_last_30_days  \\\n",
       "0           IN          Music  ...                     2000000.0   \n",
       "1           US          Games  ...                           NaN   \n",
       "2           US  Entertainment  ...                     8000000.0   \n",
       "3           US      Education  ...                     1000000.0   \n",
       "4           IN  Entertainment  ...                     1000000.0   \n",
       "\n",
       "   created_year  created_month  created_date  \\\n",
       "0        2006.0            Mar          13.0   \n",
       "1        2006.0            Mar           5.0   \n",
       "2        2012.0            Feb          20.0   \n",
       "3        2006.0            Sep           1.0   \n",
       "4        2006.0            Sep          20.0   \n",
       "\n",
       "   Gross tertiary education enrollment (%)    Population  Unemployment rate  \\\n",
       "0                                     28.1  1.366418e+09               5.36   \n",
       "1                                     88.2  3.282395e+08              14.70   \n",
       "2                                     88.2  3.282395e+08              14.70   \n",
       "3                                     88.2  3.282395e+08              14.70   \n",
       "4                                     28.1  1.366418e+09               5.36   \n",
       "\n",
       "   Urban_population   Latitude  Longitude  \n",
       "0       471031528.0  20.593684  78.962880  \n",
       "1       270663028.0  37.090240 -95.712891  \n",
       "2       270663028.0  37.090240 -95.712891  \n",
       "3       270663028.0  37.090240 -95.712891  \n",
       "4       471031528.0  20.593684  78.962880  \n",
       "\n",
       "[5 rows x 28 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Original Shape:\",df.shape)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d6b055d3-a5e8-4490-8549-4a570e58ad3d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "category                                    46\n",
       "Country                                    122\n",
       "Abbreviation                               122\n",
       "channel_type                                30\n",
       "video_views_rank                             1\n",
       "country_rank                               116\n",
       "channel_type_rank                           33\n",
       "video_views_for_the_last_30_days            56\n",
       "subscribers_for_last_30_days               337\n",
       "created_year                                 5\n",
       "created_month                                5\n",
       "created_date                                 5\n",
       "Gross tertiary education enrollment (%)    123\n",
       "Population                                 123\n",
       "Unemployment rate                          123\n",
       "Urban_population                           123\n",
       "Latitude                                   123\n",
       "Longitude                                  123\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.isnull().sum()[df.isnull().sum()>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e24fd40-17b9-4b81-bec0-7c471393daf6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "rank                                         int64\n",
       "Youtuber                                    object\n",
       "subscribers                                  int64\n",
       "video views                                float64\n",
       "category                                    object\n",
       "Title                                       object\n",
       "uploads                                      int64\n",
       "Country                                     object\n",
       "Abbreviation                                object\n",
       "channel_type                                object\n",
       "video_views_rank                           float64\n",
       "country_rank                               float64\n",
       "channel_type_rank                          float64\n",
       "video_views_for_the_last_30_days           float64\n",
       "lowest_monthly_earnings                    float64\n",
       "highest_monthly_earnings                   float64\n",
       "lowest_yearly_earnings                     float64\n",
       "highest_yearly_earnings                    float64\n",
       "subscribers_for_last_30_days               float64\n",
       "created_year                               float64\n",
       "created_month                               object\n",
       "created_date                               float64\n",
       "Gross tertiary education enrollment (%)    float64\n",
       "Population                                 float64\n",
       "Unemployment rate                          float64\n",
       "Urban_population                           float64\n",
       "Latitude                                   float64\n",
       "Longitude                                  float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c4a4d336-fb39-4d14-a23c-99812003dd06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape after removing 1970 rows: (994, 28)\n"
     ]
    }
   ],
   "source": [
    "df=df[df[\"created_year\"]!=1970]\n",
    "print(\"Shape after removing 1970 rows:\",df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e7f550c6-d8ed-4d54-957b-e8f3fef0ab35",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Shape After removing zero-upload/zero-view rows: (948, 28)\n"
     ]
    }
   ],
   "source": [
    "df=df[df[\"uploads\"]>0]\n",
    "df=df[df[\"video views\"]>0]\n",
    "print(\"Shape After removing zero-upload/zero-view rows:\",df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "54e0ddc9-b6b2-4d58-9901-09ed9bc248ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"Country\"]=df[\"Country\"].fillna(\"Unknown\")\n",
    "df[\"Abbreviation\"]=df[\"Abbreviation\"].fillna(\"N/A\")\n",
    "df[\"channel_type\"]=df[\"channel_type\"].fillna(\"Unknown\")\n",
    "df[\"category\"]=df[\"category\"].fillna(\"Unknown\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e097254b-ddb6-4f9a-b2aa-2e32119f4adc",
   "metadata": {},
   "outputs": [],
   "source": [
    "country_level_cols = [\n",
    "    \"Gross tertiary education enrollment (%)\",\n",
    "    \"Population\",\n",
    "    \"Unemployment rate\",\n",
    "    \"Urban_population\",\n",
    "    \"Latitude\",\n",
    "    \"Longitude\",\n",
    "]\n",
    "for col in country_level_cols:\n",
    "    df[col]=df[col].fillna(df[col].median())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b9f4e547-948c-4c60-ae3b-6704c24b9894",
   "metadata": {},
   "outputs": [],
   "source": [
    "rank_cols = [\"video_views_rank\", \"country_rank\", \"channel_type_rank\"]\n",
    "for col in rank_cols:\n",
    "    df[col]=df[col].fillna(df[col].median())\n",
    "df[\"video_views_for_the_last_30_days\"] = df[\"video_views_for_the_last_30_days\"].fillna(0)\n",
    "df[\"subscribers_for_last_30_days\"] = df[\"subscribers_for_last_30_days\"].fillna(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "1a8150db-9417-4636-8316-6c7d6b5b1e7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "df=df.dropna(subset=[\"created_year\",\"created_month\",\"created_date\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "e777fec4-2e8e-44b6-b97b-f05e37ae71ca",
   "metadata": {},
   "outputs": [],
   "source": [
    "df[\"created_year\"]=df[\"created_year\"].astype(int)\n",
    "df[\"created_date\"]=df[\"created_date\"].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "75f1dd57-4b6b-499e-88e1-fe708134720c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df=df.drop_duplicates()\n",
    "df=df.reset_index(drop=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "bdf7fd85-93f7-43e1-b34d-881dbf7c8381",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saved.\n",
      "   Unnamed: 0  rank                    Youtuber  subscribers   video views  \\\n",
      "0           0     1                    T-Series    245000000  2.280000e+11   \n",
      "1           2     3                     MrBeast    166000000  2.836884e+10   \n",
      "2           3     4  Cocomelon - Nursery Rhymes    162000000  1.640000e+11   \n",
      "3           4     5                   SET India    159000000  1.480000e+11   \n",
      "4           6     7         ýýý Kids Diana Show    112000000  9.324704e+10   \n",
      "\n",
      "         category                       Title  uploads        Country  \\\n",
      "0           Music                    T-Series    20082          India   \n",
      "1   Entertainment                     MrBeast      741  United States   \n",
      "2       Education  Cocomelon - Nursery Rhymes      966  United States   \n",
      "3           Shows                   SET India   116536          India   \n",
      "4  People & Blogs         ýýý Kids Diana Show     1111  United States   \n",
      "\n",
      "  Abbreviation  ... subscribers_for_last_30_days  created_year  created_month  \\\n",
      "0           IN  ...                    2000000.0          2006            Mar   \n",
      "1           US  ...                    8000000.0          2012            Feb   \n",
      "2           US  ...                    1000000.0          2006            Sep   \n",
      "3           IN  ...                    1000000.0          2006            Sep   \n",
      "4           US  ...                          0.0          2015            May   \n",
      "\n",
      "   created_date  Gross tertiary education enrollment (%)    Population  \\\n",
      "0            13                                     28.1  1.366418e+09   \n",
      "1            20                                     88.2  3.282395e+08   \n",
      "2             1                                     88.2  3.282395e+08   \n",
      "3            20                                     28.1  1.366418e+09   \n",
      "4            12                                     88.2  3.282395e+08   \n",
      "\n",
      "   Unemployment rate  Urban_population   Latitude  Longitude  \n",
      "0               5.36       471031528.0  20.593684  78.962880  \n",
      "1              14.70       270663028.0  37.090240 -95.712891  \n",
      "2              14.70       270663028.0  37.090240 -95.712891  \n",
      "3               5.36       471031528.0  20.593684  78.962880  \n",
      "4              14.70       270663028.0  37.090240 -95.712891  \n",
      "\n",
      "[5 rows x 29 columns]\n"
     ]
    }
   ],
   "source": [
    "df.to_csv(r\"C:\\Users\\daroo\\Downloads\\Global_YouTube_Statistics_Cleaned.csv\", index=False)\n",
    "print(\"Saved.\")\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2590ef93-799f-4486-9cee-0ccbc84989d1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
