Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
313 changes: 313 additions & 0 deletions your-code/.ipynb_checkpoints/main-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Before your start:\n",
"- Read the README.md file\n",
"- Comment as much as you can and use the resources in the README.md file\n",
"- Happy learning!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 1 - Working with JSON files\n",
"\n",
"Import the pandas library"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Your import here:\n",
"import pandas as pd\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### After importing pandas, let's find a dataset. In this lesson we will be working with a NASA dataset.\n",
"\n",
"Run the code in the cell below to load the dataset containing information about asteroids that have landed on earth. This piece of code helps us open the URL for the dataset and deocde the data using UTF-8."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Run this code\n",
"\n",
"from urllib.request import urlopen\n",
"import json\n",
"\n",
"response = urlopen(\"https://data.nasa.gov/resource/y77d-th95.json\")\n",
"json_data = response.read().decode('utf-8', 'replace')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the next cell, load the data in `json_data` and load it into a pandas dataframe. Name the dataframe `nasa`."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have loaded the data, let's examine it using the `head()` function."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The `value_counts()` function is commonly used in pandas to find the frequency of every value in a column.\n",
"\n",
"In the cell below, use the `value_counts()` function to determine the frequency of all types of asteroid landings by applying the function to the `fall` column."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, let's save the dataframe as a json file again. Since we downloaded the file from an online source, the goal of saving the dataframe is to have a local copy. Save the dataframe using the `orient=records` argument and name the file `nasa.json`."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 2 - Working with CSV and Other Separated Files\n",
"\n",
"csv files are more commonly used as dataframes. In the cell below, load the file from the URL provided using the `read_csv()` function in pandas. Starting version 0.19 of pandas, you can load a csv file into a dataframe directly from a URL without having to load the file first like we did with the JSON URL. The dataset we will be using contains informtaions about NASA shuttles. \n",
"\n",
"In the cell below, we define the column names and the URL of the data. Following this cell, read the tst file to a variable called `shuttle`. Since the file does not contain the column names, you must add them yourself using the column names declared in `cols` using the `names` argument. Additionally, a tst file is space separated, make sure you pass ` sep=' '` to the function."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Run this code:\n",
"\n",
"cols = ['time', 'rad_flow', 'fpv_close', 'fpv_open', 'high', 'bypass', 'bpv_close', 'bpv_open', 'class']\n",
"tst_url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/statlog/shuttle/shuttle.tst'"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's verify that this worked by looking at the `head()` function."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To make life easier for us, let's turn this dataframe into a comma separated file by saving it using the `to_csv()` function. Save `shuttle` into the file `shuttle.csv` and ensure the file is comma separated and that we are not saving the index column."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge 3 - Working with Excel Files\n",
"\n",
"We can also use pandas to convert excel spreadsheets to dataframes. Let's use the `read_excel()` function. In this case, `astronauts.xls` is in the same folder that contains this notebook. Read this file into a variable called `astronaut`. \n",
"\n",
"Note: Make sure to install the `xlrd` library if it is not yet installed."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the `head()` function to inspect the dataframe."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the `value_counts()` function to find the most popular undergraduate major among all astronauts."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Due to all the commas present in the cells of this file, let's save it as a tab separated csv file. In the cell below, save `astronaut` as a tab separated file using the `to_csv` function. Call the file `astronaut.csv` and remember to remove the index column."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bonus Challenge - Fertility Dataset\n",
"\n",
"Visit the following [URL](https://archive.ics.uci.edu/ml/datasets/Fertility) and retrieve the dataset as well as the column headers. Determine the correct separator and read the file into a variable called `fertility`. Examine the dataframe using the `head()` function."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading