\n",
"\n",
"Estimated time needed: **60** minutes.\n",
"\n",
"## Introduction\n",
"\n",
"Using this Python notebook you will:\n",
"\n",
"1. Understand the Spacex DataSet\n",
"2. Load the dataset into the corresponding table in a Db2 database\n",
"3. Execute SQL queries to answer assignment questions\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d1tyTG7gNtdW"
},
"source": [
"## Overview of the DataSet\n",
"\n",
"SpaceX has gained worldwide attention for a series of historic milestones.\n",
"\n",
"It is the only private company ever to return a spacecraft from low-earth orbit, which it first accomplished in December 2010.\n",
"SpaceX advertises Falcon 9 rocket launches on its website with a cost of 62 million dollars wheras other providers cost upward of 165 million dollars each, much of the savings is because Space X can reuse the first stage.\n",
"\n",
"Therefore if we can determine if the first stage will land, we can determine the cost of a launch.\n",
"\n",
"This information can be used if an alternate company wants to bid against SpaceX for a rocket launch.\n",
"\n",
"This dataset includes a record for each payload carried during a SpaceX mission into outer space.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tX_l4RMINtdX"
},
"source": [
"### Download the datasets\n",
"\n",
"This assignment requires you to load the spacex dataset.\n",
"\n",
"In many cases the dataset to be analyzed is available as a .CSV (comma separated values) file, perhaps on the internet. Click on the link below to download and save the dataset (.CSV file):\n",
"\n",
"Spacex DataSet\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vbNpE34zNtdX"
},
"source": [
"### Store the dataset in database table\n",
"\n",
"**it is highly recommended to manually load the table using the database console LOAD tool in DB2**.\n",
"\n",
"\n",
"\n",
"Now open the Db2 console, open the LOAD tool, Select / Drag the .CSV file for the dataset, Next create a New Table, and then follow the steps on-screen instructions to load the data. Name the new table as follows:\n",
"\n",
"**SPACEXDATASET**\n",
"\n",
"**Follow these steps while using old DB2 UI which is having Open Console Screen**\n",
"\n",
"**Note:While loading Spacex dataset, ensure that detect datatypes is disabled. Later click on the pencil icon(edit option).**\n",
"\n",
"1. Change the Date Format by manually typing DD-MM-YYYY and timestamp format as DD-MM-YYYY HH\\:MM:SS\n",
"\n",
"2. Change the PAYLOAD_MASS\\_\\_KG\\_ datatype to INTEGER.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tIW3H1JjNtdY"
},
"source": [
"**Changes to be considered when having DB2 instance with the new UI having Go to UI screen**\n",
"\n",
"* Refer to this insruction in this link for viewing the new Go to UI screen.\n",
"\n",
"* Later click on **Data link(below SQL)** in the Go to UI screen and click on **Load Data** tab.\n",
"\n",
"* Later browse for the downloaded spacex file.\n",
"\n",
"\n",
"\n",
"* Once done select the schema andload the file.\n",
"\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "xv9m4QQKNtdZ"
},
"outputs": [],
"source": [
"!pip install sqlalchemy==1.3.9\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0gXX3srdNtda"
},
"source": [
"### Connect to the database\n",
"\n",
"Let us first load the SQL extension and establish a connection with the database\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "2yv8wJ1INtdb"
},
"outputs": [],
"source": [
"%load_ext sql"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "RiARoe8bNtdb"
},
"outputs": [],
"source": [
"import csv, sqlite3\n",
"\n",
"con = sqlite3.connect(\"my_data1.db\")\n",
"cur = con.cursor()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "aPCWc-v9Ntdc",
"outputId": "211a41cf-538e-4697-898f-616b18b802b1"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"\u001b[K |████████████████████████████████| 9.5 MB 8.2 MB/s \n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -q pandas==1.1.5"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"id": "4mR0SCpTNtdc",
"outputId": "9ee4c1c1-265c-40a1-dd52-7a3ee5b1e82e"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Connected: @my_data1.db'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"%sql sqlite:///my_data1.db"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "WxR32qmiNtdd"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"df = pd.read_csv(\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DS0321EN-SkillsNetwork/labs/module_2/data/Spacex.csv\")\n"
]
},
{
"cell_type": "code",
"source": [
"df.to_sql(\"SPACEXTBL\", con, if_exists='replace', index=False,method=\"multi\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "YbuU8btUOS6N",
"outputId": "c67c9bec-b8f3-4a16-b78a-d02ef1ec5396"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py:2615: UserWarning: The spaces in these column names will not be changed. In pandas versions < 0.14, spaces were converted to underscores.\n",
" method=method,\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 354
},
"id": "SOYaq1JUNtdd",
"outputId": "98690e7d-94c3-4a6f-bb0d-884beb135ef5"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
" Date Time (UTC) Booster_Version Launch_Site \\\n",
"0 04-06-2010 18:45:00 F9 v1.0 B0003 CCAFS LC-40 \n",
"1 08-12-2010 15:43:00 F9 v1.0 B0004 CCAFS LC-40 \n",
"2 22-05-2012 07:44:00 F9 v1.0 B0005 CCAFS LC-40 \n",
"3 08-10-2012 00:35:00 F9 v1.0 B0006 CCAFS LC-40 \n",
"4 01-03-2013 15:10:00 F9 v1.0 B0007 CCAFS LC-40 \n",
"\n",
" Payload PAYLOAD_MASS__KG_ \\\n",
"0 Dragon Spacecraft Qualification Unit 0 \n",
"1 Dragon demo flight C1, two CubeSats, barrel of... 0 \n",
"2 Dragon demo flight C2 525 \n",
"3 SpaceX CRS-1 500 \n",
"4 SpaceX CRS-2 677 \n",
"\n",
" Orbit Customer Mission_Outcome Landing _Outcome \n",
"0 LEO SpaceX Success Failure (parachute) \n",
"1 LEO (ISS) NASA (COTS) NRO Success Failure (parachute) \n",
"2 LEO (ISS) NASA (COTS) Success No attempt \n",
"3 LEO (ISS) NASA (CRS) Success No attempt \n",
"4 LEO (ISS) NASA (CRS) Success No attempt "
],
"text/html": [
"\n",
"