Featured Article

Effortless Transfer pandas dataframe to mongodb using pymongo Guide

Kenneth Jul 13, 2026

When working with data, pandas and PyMongo are two powerful tools that often go hand in hand. pandas is a data manipulation library in Python, while PyMongo is a Python driver for MongoDB, the popular NoSQL database. If you're working with pandas DataFrame and need to interact with a MongoDB database, this guide will walk you through the process.

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

Before we dive in, ensure you have both pandas and PyMongo installed. If you haven't already, you can install them using pip:

Pandas GroupBy: Group, Summarize, and Aggregate Data in Python
Pandas GroupBy: Group, Summarize, and Aggregate Data in Python

```bash pip install pandas pymongo ```

Installing and Connecting to MongoDB with PyMongo

First, let's discuss how to install and connect to a MongoDB database using PyMongo.

Pandas DataFrame - GeeksforGeeks
Pandas DataFrame - GeeksforGeeks

Installation

PyMongo is usually installed alongside the Python interpreter. If it's not, you can install it using pip:

Pandas DataFrame to a List in Python
Pandas DataFrame to a List in Python

```bash pip install pymongo ```

Connection

To connect to a MongoDB database, you use the `MongoClient` class from PyMongo. Here's a simple example:

```python from pymongo import MongoClient # Create a client to the MongoDB instance running on localhost:27017 client = MongoClient('mongodb://localhost:27017/') ```

For production use, you should consider using a URI to connect to your MongoDB instance, which can include authentication details and connection options.

an info sheet with the words important pandas method for data science and other information
an info sheet with the words important pandas method for data science and other information

Converting pandas DataFrame to MongoDB Documents

Now that we're connected to our MongoDB database, let's look at how to convert a pandas DataFrame into MongoDB documents.

MongoDB Collection

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

In MongoDB, a collection is similar to a table in SQL. Before inserting our pandas DataFrame, we need to select or create a collection using the `db` object from our `MongoClient`:

```python db = client.mydatabase # replace with your database name collection = db.mycollection # replace with your collection name ```

DataFrame to Document

a table with the words important pandas and other symbols
a table with the words important pandas and other symbols
Pandas data correlations cheat sheet
Pandas data correlations cheat sheet
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
Overview of Pandas Data Types
Overview of Pandas Data Types
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: Mastering Data Manipulation and Analysis
Pandas Tutorials: Mastering Data Manipulation and Analysis
Sort a Pandas DataFrame
Sort a Pandas DataFrame
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
Pandas and Key Features
Pandas and Key Features

Now we can convert our DataFrame to a MongoDB document. We'll use the `to_dict` method and specify `orient='records'` to achieve this:

```python import pandas as pd # Assume df is your pandas DataFrame documents = df.to_dict(orient='records') ```

Prepare to insert these documents into your MongoDB collection:

Inserting Documents

PyMongo provides several methods for inserting documents into a collection. The `insert_many` method allows us to insert our list of documents:

```python # Insert documents into the MongoDB collection collection.insert_many(documents) ```

Remember to replace `df`, `client`, `db`, and `collection` with your actual DataFrame, MongoClient, database, and collection.

Retrieving Documents and Converting to DataFrame

You may also need to retrieve documents from MongoDB and convert them back into a pandas DataFrame. Let's discuss how to do this.

Finding Documents

PyMongo provides several ways to find documents in a collection. The simplest form is using `find`, which returns a cursor to the result. We can convert this cursor to a list of documents:

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

Cursor to DataFrame

The `pandas.DataFrame` constructor can take a list of dictionaries as input, so we can easily convert our list of documents to a DataFrame:

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

This converts our list of documents to a pandas DataFrame, ready for further data manipulation or analysis.

As with any data manipulation task, remember to handle your data carefully. Always ensure you're working with the correct data, especially when reading and writing from databases.