{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "25ab3466-e425-4e98-9c72-2308c461e928",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-e097c96d7da2f907",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "# Analysing Uber Trips\n",
    "### Supervised by: Siddarth Marwaha (Sid); Email: marwaha@ifn.ing.tu-bs.de\n",
    "\n",
    "Uber has become one of the most famous unicorn companies, offering its services to more than 80 countries worldwide. To serve their customers better and provide uninterruptible services, they want to analyse the data (Uber rides) collected over time to detect patterns, such as the busiest day or the time with the lowest number of rides. \n",
    "\n",
    "This project offers the opportunity to get familiar with the common steps of analyzing a dataset and how to draw insights from data through visualizing the relationship between different variables, solidifying the foundational rules of coding in Python.\n",
    "\n",
    "What will You Learn from This Python Project?\n",
    "* Read a dataset and display records from it\n",
    "* Use your detective’s hat and uncover hidden patterns in data\n",
    "* Find the relationship between different variables\n",
    "* Draw insights by visualizing these relationships"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b38edc7",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-d78a248f4ecd4419",
     "locked": true,
     "schema_version": 3,
     "solution": false,
     "task": false
    },
    "tags": []
   },
   "source": [
    "## Perform the following tasks: \n",
    "\n",
    "* Import the necessary libraries \n",
    "* Read the dataset and explore the dataset via the following properties - shape, size, info, head, tail\n",
    "* Break the Date/Time column to \"Day\", \"Hour\", & \"Weekday\" \n",
    "    * change the \"Date/Time\" column's data type from string to datetime\n",
    "    * convert \"Date/Time\" column from string data type into DateTime\n",
    "* Plot (bar plot) the density of rides according to days, hours, and weekdays\n",
    "    * Identify what days of the week are the number of rides highest and lowest \n",
    "* Plot (bar plot) the density of rides per weekdays\n",
    "    * Identify the most and least busy day of the week\n",
    "* Plot (bar plot) density of rides per hour\n",
    "    * Idensity the hours with the highest and least number of rides\n",
    "* Plot (scatter plot) density of rides per location\n",
    "* Use Foliums Polyline Feature to Map out the Trips"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "04636cfa",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-98a99458889234cd",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "### BEGIN SOLUTION\n",
    "# To read the dataset\n",
    "import pandas as pd \n",
    "# For visualization\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline \n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "56211e68",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-1c967a95c9b52343",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          Date/Time      Lat      Lon    Base\n",
      "0  9/1/2014 0:01:00  40.2201 -74.0021  B02512\n",
      "1  9/1/2014 0:01:00  40.7500 -74.0027  B02512\n",
      "2  9/1/2014 0:03:00  40.7559 -73.9864  B02512\n",
      "3  9/1/2014 0:06:00  40.7450 -73.9889  B02512\n",
      "4  9/1/2014 0:11:00  40.8145 -73.9444  B02512\n",
      "                  Date/Time      Lat      Lon    Base\n",
      "1028131  9/30/2014 22:57:00  40.7668 -73.9845  B02764\n",
      "1028132  9/30/2014 22:57:00  40.6911 -74.1773  B02764\n",
      "1028133  9/30/2014 22:58:00  40.8519 -73.9319  B02764\n",
      "1028134  9/30/2014 22:58:00  40.7081 -74.0066  B02764\n",
      "1028135  9/30/2014 22:58:00  40.7140 -73.9496  B02764\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 1028136 entries, 0 to 1028135\n",
      "Data columns (total 4 columns):\n",
      " #   Column     Non-Null Count    Dtype  \n",
      "---  ------     --------------    -----  \n",
      " 0   Date/Time  1028136 non-null  object \n",
      " 1   Lat        1028136 non-null  float64\n",
      " 2   Lon        1028136 non-null  float64\n",
      " 3   Base       1028136 non-null  object \n",
      "dtypes: float64(2), object(2)\n",
      "memory usage: 31.4+ MB\n",
      "None\n",
      "(1028136, 4)\n",
      "                Lat           Lon\n",
      "count  1.028136e+06  1.028136e+06\n",
      "mean   4.073922e+01 -7.397182e+01\n",
      "std    4.082861e-02  5.831413e-02\n",
      "min    3.998970e+01 -7.477360e+01\n",
      "25%    4.072040e+01 -7.399620e+01\n",
      "50%    4.074180e+01 -7.398310e+01\n",
      "75%    4.076120e+01 -7.396280e+01\n",
      "max    4.134760e+01 -7.271630e+01\n"
     ]
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "uber_df= pd.read_csv(\"uber-raw-data-sep14.csv\")\n",
    "print(uber_df.head(5))\n",
    "print(uber_df.tail())\n",
    "print(uber_df.info())\n",
    "print(uber_df.shape)\n",
    "print(uber_df.describe())\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6eab0dcf",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-0611a5d0b5ec61a1",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "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>Date/Time</th>\n",
       "      <th>Lat</th>\n",
       "      <th>Lon</th>\n",
       "      <th>Base</th>\n",
       "      <th>Day</th>\n",
       "      <th>Hour</th>\n",
       "      <th>Weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1028131</th>\n",
       "      <td>2014-09-30 22:57:00</td>\n",
       "      <td>40.7668</td>\n",
       "      <td>-73.9845</td>\n",
       "      <td>B02764</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1028132</th>\n",
       "      <td>2014-09-30 22:57:00</td>\n",
       "      <td>40.6911</td>\n",
       "      <td>-74.1773</td>\n",
       "      <td>B02764</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1028133</th>\n",
       "      <td>2014-09-30 22:58:00</td>\n",
       "      <td>40.8519</td>\n",
       "      <td>-73.9319</td>\n",
       "      <td>B02764</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1028134</th>\n",
       "      <td>2014-09-30 22:58:00</td>\n",
       "      <td>40.7081</td>\n",
       "      <td>-74.0066</td>\n",
       "      <td>B02764</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1028135</th>\n",
       "      <td>2014-09-30 22:58:00</td>\n",
       "      <td>40.7140</td>\n",
       "      <td>-73.9496</td>\n",
       "      <td>B02764</td>\n",
       "      <td>30</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  Date/Time      Lat      Lon    Base  Day  Hour  Weekday\n",
       "1028131 2014-09-30 22:57:00  40.7668 -73.9845  B02764   30    22        1\n",
       "1028132 2014-09-30 22:57:00  40.6911 -74.1773  B02764   30    22        1\n",
       "1028133 2014-09-30 22:58:00  40.8519 -73.9319  B02764   30    22        1\n",
       "1028134 2014-09-30 22:58:00  40.7081 -74.0066  B02764   30    22        1\n",
       "1028135 2014-09-30 22:58:00  40.7140 -73.9496  B02764   30    22        1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Change the \"Date/Time\" column's data type from string to datetime\n",
    "uber_df['Date/Time']= pd.to_datetime(uber_df['Date/Time'])\n",
    "\n",
    "# Convert \"Date/Time\" column from string data type into DateTime\n",
    "uber_df[\"Day\"] = uber_df[\"Date/Time\"].apply(lambda x: x.day)\n",
    "uber_df[\"Hour\"] = uber_df[\"Date/Time\"].apply(lambda x: x.hour)\n",
    "uber_df[\"Weekday\"] = uber_df[\"Date/Time\"].apply(lambda x: x.weekday())\n",
    "uber_df.tail(5)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "890e8cb2",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-39e52dc95c4cb1bc",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Density of rides')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 28856 characters]\n",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Visualize the Density of rides per Day of month\n",
    "fig,ax = plt.subplots(figsize = (12,6))\n",
    "plt.hist(uber_df.Day, width= 0.6, bins= 30)\n",
    "plt.title(\"Density of trips per Day\", fontsize=16)\n",
    "plt.xlabel(\"Day\", fontsize=14)\n",
    "plt.ylabel(\"Density of rides\", fontsize=14)\n",
    "# From the above plot we can notice that the highest number of rides are during working days (Monday to Friday), \n",
    "# while the least number of rides are in weekends.\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2c748d00",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-a663d5944372e8e3",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Density of rides')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 37904 characters]\n",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Visualize the Density of rides per Weekday\n",
    "fig,ax = plt.subplots(figsize = (12,6))\n",
    "plt.hist(uber_df.Weekday, width= 0.6, range= (0, 6.5), bins=7, color= \"green\")\n",
    "plt.title(\"Density of trips per Weekday\", fontsize=16)\n",
    "plt.xlabel(\"Weekday\", fontsize=14)\n",
    "plt.ylabel(\"Density of rides\", fontsize=14)\n",
    "### END SOLUTION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ab73054b",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-f4a0ad1d5ee74db4",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Density of rides')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 31372 characters]\n",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "#  Density of rides per hour\n",
    "fig,ax = plt.subplots(figsize = (12,6))\n",
    "plt.hist(uber_df.Hour, width= 0.6, bins=24, color= \"orange\")\n",
    "plt.title(\"Density of trips per Hour\", fontsize=16)\n",
    "plt.xlabel(\"Hour\", fontsize=14)\n",
    "plt.ylabel(\"Density of rides\", fontsize=14)\n",
    "# It seems like the number of rides decrease gradually from 1 AM to 4 AM and then increases starting from 5 AM onward till it reaches 6 PM \n",
    "# which is the hour with the highest number of rides.\n",
    "### END SOLUTION "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5e4af60e",
   "metadata": {
    "nbgrader": {
     "grade": false,
     "grade_id": "cell-6f80242169081174",
     "locked": false,
     "schema_version": 3,
     "solution": true,
     "task": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0, 0.5, 'Density of rides')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 90296 characters]\n",
      "text/plain": [
       "<Figure size 1200x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "### BEGIN SOLUTION\n",
    "# Density of rides per location\n",
    "fig,ax = plt.subplots(figsize = (12,6))\n",
    "x= uber_df.Lon\n",
    "y= uber_df.Lat\n",
    "plt.scatter(x, y, color= \"purple\")\n",
    "plt.title(\"Density of trips per Hour\", fontsize=16)\n",
    "plt.xlabel(\"Longitude/Latitude\", fontsize=14)\n",
    "plt.ylabel(\"Density of rides\", fontsize=14)\n",
    "# The region with the highest density of rides is near Manhattan and Newburgh. While the region with the lowest density is near New Jersey.\n",
    "### END SOLUTION"
   ]
  }
 ],
 "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.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
