Assignment 5a: Pandas Fundamentals with Earthquake Data#
At-home assignment — worth 10 points. When you’re done, push your work to your week folder and post a link to your completed notebook on the matching Courseworks assignment.
In this assignment you’ll exercise core pandas skills on a real-world dataset: the USGS Earthquakes catalog.
Learning goals
This assignment exercises the pandas fundamentals from this section:
Load CSV data directly from a URL into a
DataFrameParse dates and use them as a time index
Use
describe,nlargest, and column selection to explore dataFilter rows with boolean masks
Create a new column derived from existing ones
Group by a column and count entries
Plot histograms and scatter plots directly from a DataFrame
Working through this notebook
Download this notebook using the ⬇ button in the top-right (or copy-paste the cells into a fresh notebook), open it in your environment (JupyterLab on LEAP or Colab), and fill in your solution under each numbered task.
When you’re done, follow the submission instructions at the bottom of the page.
Start by importing numpy, pandas, and matplotlib’s pyplot (with conventional short aliases). The cell below is empty — type your imports there and run it.
Data for this assignment in .csv format downloaded from the USGS Earthquakes Database is available at:
You don’t need to download this file. You can open it directly with Pandas.
1) Use Pandas’ read_csv function directly on this URL to open it as a DataFrame#
(Don’t use any special options). Display the first few rows and the DataFrame info.
You should have seen that the dates were not automatically parsed into datetime types.
2) Re-read the data in such a way that all date columns are identified as dates and the earthquake ID is used as the index#
Verify that this worked using the head and info functions.
3) Use describe to get the basic statistics of all the columns#
Note the highest and lowest magnitude of earthquakes in the databse.
4) Use nlargest to get the top 20 earthquakes by magnitude#
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.nlargest.html
Examine the structure of the place column. The state / country information seems to be in there. How would you get it out?
5) Extract the state or country using Pandas text data functions#
Add it as a new column to the dataframe called country. Note that some of the “countries” are actually U.S. states.
6) Display each unique value from the new column#
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.unique.html
7) Create a filtered dataset that contains only earthquakes with magnitude greater than 4#
Store it in a new variable (e.g. df_big) — you’ll reuse it in the next tasks.
8) Using the filtered dataset (magnitude > 4), count the number of earthquakes in each country/state. Make a bar chart of this number for the top 5 locations with the most earthquakes#
Location name on the x axis, Earthquake count on the y axis
9) Make a histogram the distribution of the Earthquake magnitudes#
https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.hist.html https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hist.html
Do one subplot for the filtered and one for the unfiltered dataset. Use a Logarithmic scale. What sort of relationship do you see?
10) Visualize the locations of earthquakes by making a scatterplot of their latitude and longitude#
Use a two-column subplot with both the filtered and unfiltered datasets. Color the points by magnitude. Make it pretty
What difference do you note between the filtered and unfiltered datasets?
Submission instructions#
When you’re done, save your completed notebook as assignment5a.ipynb inside the current week’s folder in your private clmt5405-assignments GitHub repo. Then push the commit:
git add <weekN>/assignment5a.ipynb
git commit -m "Submit assignment 5a"
git push
Due Sunday night.