Question
5. Download the Pittsburgh city fire incident data from the class web page. Using Jupyter notebook, write a Python program that performs the following steps.
5. Download the Pittsburgh city fire incident data from the class web page. Using Jupyter notebook, write a Python program that performs the following steps.
(a) Use pandas to load the CSV file into a DataFrame object named data. After loading the data successfully:
i. Type data.head() to return the first 5 records of the dataset.
Solution:
In [ ]:
import pandas as pd data = ... data.head()
ii. Determine the number of rows (data records) and columns (attributes) of the dataset using the data.shape command.
Solution:
In [ ]:
iii. Count the frequency of each type of fire incident by applying the value_counts() function to the type description column (see the pandas example given in lecture 2).
Solution:
(b) Using pymongo, write the Python code for storing the data into MongoDB. Specifically, create a collection named fire, where each document corresponds to a fire incident record. You must first convert each record into a dictionary object before inserting it into MongoDB (see the example given in Exercise 3). Each fire incident record should be stored as a set of key-value pairs in BSON format on MongoDB, where the keys should correspond to the attribute names of the original CSV data file.
Solution:
(c) Using pymongo, execute a MongoDB query that identifies the top-5 most frequent type of fire incidents. Your result should be consistent with the answer for Question 5(a)(iii). Hint: you will need to use the following MongoDB operators for the query: group, sort, and $limit. See the example given in https://www.mkyong.com/mongodb/mongodb-aggregate-and-group-example/ on how to write queries with aggregate functions. Note that aggregate functions are implemented in pymongo as follows:
db..aggregate( ... )
Solution:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started