Featured Article

Effortlessly Transfer Pandas DataFrame to MongoDB in Minutes

Kenneth Jul 13, 2026

When working with data in Python, you'll often find yourself using Pandas DataFrames for data manipulation and analysis. However, when it comes to storing and managing this data, MongoDB - a popular NoSQL database - is an excellent choice, especially for its flexibility and scalability. In this guide, we'll explore how to convert a Pandas DataFrame to MongoDB using the pymongo driver.

How to Merge Pandas Dataframe
How to Merge Pandas Dataframe

Before we dive in, ensure you have pymongo and pandas installed in your Python environment. If not, you can install them using pip:

Insert pandas dataframe into Mongodb
Insert pandas dataframe into Mongodb

pip install pymongo pandas

Establishing Connection with MongoDB

Create Pandas DataFrame from a Numpy Array
Create Pandas DataFrame from a Numpy Array

The first step is to establish a connection with your MongoDB instance. Here's how you can do it:

Connecting to MongoDB

Pandas Data Manipulation: A Comprehensive Guide
Pandas Data Manipulation: A Comprehensive Guide

Connecting to MongoDB from Python involves using the MongoClient class from the pymongo library. You can specify the connection string, which includes the hostname and port number of your MongoDB instance.

```python from pymongo import MongoClient client = MongoClient("mongodb://localhost:27017/") ```

Creating a Database and Collection

Displaying a DataFrame in Python
Displaying a DataFrame in Python

After establishing a connection, you can create a new database or use an existing one. You can also create a new collection (similar to tables in SQL) in the database.

```python db = client["mydatabase"] collection = db["mycollection"] ```

Converting Pandas DataFrame to MongoDB

Python Pandas DataFrame: load, edit, view data
Python Pandas DataFrame: load, edit, view data

With the connection and collection set up, you can now insert your Pandas DataFrame into MongoDB. Pandas DataFrames are similar to lists of dictionaries in Python, which can be easilyinserted into MongoDB.

DataFrame to Dict

Pandas plotting guide for beginners
Pandas plotting guide for beginners
Pandas DataFrame to a List in Python
Pandas DataFrame to a List in Python
Pandas Tutorials: Mastering Data Manipulation and Analysis
Pandas Tutorials: Mastering Data Manipulation and Analysis
Pandas GroupBy: Group, Summarize, and Aggregate Data in Python
Pandas GroupBy: Group, Summarize, and Aggregate Data in Python
Pandas Cheat Sheet for Beginners | Data Cleaning, Filtering, GroupBy & CSV Quick Guide
Pandas Cheat Sheet for Beginners | Data Cleaning, Filtering, GroupBy & CSV Quick Guide
Pandas Tutorials: Unlock the Power of Data Analysis 🔍
Pandas Tutorials: Unlock the Power of Data Analysis 🔍
the pandas worksheet is filled with information about data and other important things
the pandas worksheet is filled with information about data and other important things
Pandas Cheat Sheet for Data Analysts (Chapter 3/15) | Python for Data Analysts
Pandas Cheat Sheet for Data Analysts (Chapter 3/15) | Python for Data Analysts
a table with the words important pandas and other symbols
a table with the words important pandas and other symbols

Pandas DataFrames can be easily converted to a list of dictionaries using the to_dict function:

```python import pandas as pd data = { 'Name': ['Tom', 'Nick', 'John'], 'Age': [20, 21, 19] } df = pd.DataFrame(data) documents = df.to_dict(orient='records') ```

Inserting DataFrame into MongoDB

Now, you can insert these documents into your MongoDB collection:

```python result = collection.insert_many(documents) ```

Querying Data from MongoDB to Pandas DataFrame

Just like you can convert a Pandas DataFrame to MongoDB, you can also retrieve data from MongoDB and convert it back to a Pandas DataFrame.

Querying Data from MongoDB

The find method in pymongo is used to query documents from a collection:

```python documents = collection.find() ```

Dict to DataFrame

The retrieved documents are a list of dictionaries, which can be easily converted back to a Pandas DataFrame using the DataFrame constructor:

```python df = pd.DataFrame(list(documents)) ```

And that's a wrap! You've now learned how to convert Pandas DataFrame to MongoDB and vice versa. This skill will greatly enhance your data manipulation and storage capabilities in your Python projects.