To create a Pandas DataFrame from a MongoDB query, we will leverage our knowledge of creating MongoDB queries to get the information that we want.
Before running a query against MongoDB, determine the information you want to look at. By creating a query filter, you will save time by only retrieving the information that you want. This is very important when you have millions or billions of rows of data.
The following code can be run in an IPython Notebook or copied/pasted into a standalone Python script:
To create a Pandas DataFrame from a MongoDB query, the first thing we need to do is import the Python libraries that we need:
import pandas as pd from pymongo import MongoClient
Next, create a connection to the MongoDB database:
client = MongoClient('localhost', 27017)
After that, use the connection we just created, and select the database and collection to query:
db = client.pythonbicookbook collection = db.accidents
Next...