How to connect and import data into DataFrame from MongoDB in python
Hi Folks,
How to connect and import data into DataFrame from MongoDB in python
Hi Folks,
Being a DataScicetist it’s not possible to get your desired data in an easy peasy CSV file sometimes we need to do some efforts to get our desired data from the database instead of a CSV file. In the world of non-relational databases, MongoDB is the most famous these days and in this blog, we will learn how to connect and import data into a data frame from MongoDB.

Connect and import data from MongoDB
The first step to getting data from a database is to get connected with the database, it can be on a remote server or in our local system on localhost. Let’s install our pymongo package to connect and query with MongoDB.
#command to install pymongo on our system
pip install pymongo
PyMongo package is installed, and now we can use it in our code. let’s create a client object to connect with the DataBase. We will import MongoClient from pymongo and connect with a database on our localhost port of 27017.
from pymongo import MongoClient
import pandas as pd
#connecting with database
client = MongoClient('localhost', 27017)
When we run this command on a successful case it will be connected with our database and now we are ready to play with the database data.
#to get a complete database
db = client["database_name"]
#to get all collections list
db.list_collection_names()
#to get a particular collection data
collections = db["test_collection_name"]
As you can see above we can query and fetch data from the Database, one important thing to remember here is mongo always returns an iterative instead of all data together you can use for loop to access any index of data.
Now we can get our desired iterative data from mongo or we can query our desired data as we use the find and find_one in Mongo.
Let’s prepare a DataFrame from the MongoDB data.
collection = db["test_collection"]
result = collection.find({"age":24,},
projection={"name":1,"_id":0})
df = pd.DataFrame(result)
df.head()
Our data frame is now ready to get some useful insights. That’s how you can connect, query, and prepare your data frame by getting data directly from MongoDB.
One last important thing is the get better visuals of MongoDB data you need to get PrettyPrinter from pprint package.
from pprint import PrettyPrinter
pp = PrettyPrinter(indent=2)
#print pretify data
pp.pprint(data)
That’s all you need to connect and get your data from MongoDB in python.
If you got any errors or have any queries feel free to ask me.
We can connect on Linkedin Too.
Important Links:
메타데이터
- post_id
- 8e6ad4b3ca8f
- slug
- how-to-connect-and-import-data-into-dataframe-from-mongodb-in-python-8e6ad4b3ca8f
- url
- https://medium.com/@farhanaslam910/how-to-connect-and-import-data-into-dataframe-from-mongodb-in-python-8e6ad4b3ca8f
- canonical_url
- https://medium.com/@farhanaslam910/how-to-connect-and-import-data-into-dataframe-from-mongodb-in-python-8e6ad4b3ca8f
- author_url
- https://medium.com/@farhanaslam910
- status
- ok
- fetched_at
- 2026-07-06 21:09:28