Prerequisites: MongoDB Python Basics
This article is about converting the PyMongo Cursor to Pandas Dataframe. Functions like find() and find_one() returns the Cursor instance.
- Importing Required Modules: Import the required module using the command:
from pymongo import MongoClient from pandas import DataFrame
If MongoDB is already not installed on your machine you can refer to the guide: Guide to Install MongoDB with Python
If pandas not install you can install it using pip and if you are using Python3 then use pip3 instead of pip to install the required modules.
pip install pandas
- Creating a Connection: Now we had already imported the module, its time to establish a connection to the MongoDB server, presumably which is running on localhost (host name) at port 27017 (port number).
client = MongoClient(‘localhost’, 27017)
- Accessing the Database: Since the connection to the MongoDB server is established. We can now create or use the existing database.
mydatabase = client.name_of_the_database
- Accessing the Collection: We now select the collection from the database using the following syntax:
collection_name = mydatabase.name_of_collection
- Getting the documents: Getting all the documents from the collection using find() method. It returns the instance of the Cursor.
cursor = collection_name.find()
- Converting the Cursor to Dataframe: Converting the Cursor to the Pandas Dataframe.
First, we convert the cursor to the list of dictionary.
list_cur = list(cursor)
Now, converting the list to the Dataframe
df = DataFrame(list_cur)
Below is the implementation.
Type of cursor: <class 'pymongo.cursor.Cursor'> Type of df: <class 'pandas.core.frame.DataFrame'> _id name Roll No Branch 0 1 Vishwash 1001 CSE 1 2 Vishesh 1002 IT 2 3 Shivam 1003 ME 3 4 Yash 1004 ECE 4 5 Raju 1005 CSE
- Convert PyMongo Cursor to JSON
- What is a PyMongo Cursor?
- How to check if the PyMongo Cursor is Empty?
- Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
- Convert given Pandas series into a dataframe with its index as another column on the dataframe
- Get the Names of all Collections using PyMongo
- Update all Documents in a Collection using PyMongo
- Difference Between insert(), insertOne(), and insertMany() in Pymongo
- Get all the information of a Collection's indexes using PyMongo
- How to rebuild all the indexes of a collection using PyMongo?
- Nested Queries in PyMongo
- Get all the Documents of the Collection using PyMongo
- How to Drop all the indexes in a Collection using PyMongo?
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- PyQt5 QSpinBox - Setting cursor
- PyQt5 QSpinBox - Accessing the cursor
- PyQt5 QSpinBox - How to unset the cursor
- PyQt5 QCalendarWidget - Changing Cursor Shape
- PyQt5 QCalendarWidget - Getting Cursor Shape
- PyQt5 QCalendarWidget - Making Cursor Shape back to normal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.