Creating a frequency table for a single column is good; creating a frequency table for two is even better. That's what we'll do in this recipe.
As with the previous recipes where we retrieve data from MongoDB, you need to have MongoDB running and to have imported the accidents
dataset.
To generate a frequency table for two variables, begin by import the required libraries as follows:
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(data...