A frequency table is another way of summarizing data; it shows the number of times a value occurs. In this recipe, we will create a frequency table of casualties by date.
To use this recipe, you need to have MongoDB running, and to have the accidents data imported.
To generate a frequency table for a single column by date, begin by importing the Python libraries that we need:
import pandas as pd import numpy as np from pymongo import MongoClient
Next, connect to MongoDB, and run a query specifying the five fields to be retrieved from the MongoDB data:
client = MongoClient('localhost', 27017) db = client.pythonbicookbook collection = db.accidents fields = {'Date':1, 'Police_Force':1, 'Accident_Severity':1, 'Number_of_Vehicles':1, 'Number_of_Casualties':1} data = collection.find({}, fields)
Next, create a DataFrame from the results of the query:
accidents = pd.DataFrame(list...