Exporting Data from Pandas Dataframe in Python to Mongo DB

Vinodnethichinna
Jun 17 · 3 min read
Image for post
Image for post

Let us discuss how we are going to export data from Python to Mongo DB. To proceed further we need to have an account in MongoDB. if you don't have you can create one by clicking .

To connect to database from Python we need a connection string to access our database, follow the below steps to find the string:

  1. Create an account in MongoDB.com.
  2. Under Clusters-> Choose Connect.
  3. After clicking on Connect you will be able to see the below screenshot.
Image for post
Image for post

4. Choose Connect to application and select Driver as “Python” & Choose the correct version of Python installed in your system.

Image for post
Image for post

5. Copy the link and make sure to replace username and password in the connection string.

Now we are good and we will see the required code to push our data from Python dataframe to Mongo DB.

Open your Jupyter Notebook and run the below code. In the very first step, we will import the required libraries. To integrate Python with MongoDB we have a library called pymongo. If you don't have a pymongo module installed, run the below line to install.

Image for post
Image for post

Once you installed import the below libraries.

Image for post
Image for post

Now you need to use the connection string with username and password in the below line of code.

Image for post
Image for post

Now we need to connect to your database and required collection in MongoDB.

Image for post
Image for post

Here replace your database name and collection name.,for example, I am using ‘project’ as my DB name and ‘NewDB’ as my collection name. Make sure to update those details accordingly.

Now we need to convert our data into dictionary format because MongoDB accepts data in JSON format (key, value pairs) that is nothing but Python dictionary. The below line of code converts the dataframe to the dictionary.

Image for post
Image for post

Here ‘data’ is my pandas dataframe name, make sure to change it accordingly. data_dict is the dictionary format of our data, now we are all set to upload our data to MongoDB.

Image for post
Image for post

This inserts our data into MongoDB, Now you can check your records in your database. Now you should be able to insert data into MongoDB from Python.

Please feel to share or comment if there are any mistakes/queries. I hope you all enjoyed reading this article.

Thank you.

Python In Plain English

Go deeper with the language powering everything.

Vinodnethichinna

Written by

Technical, Enthusiastic and Organized Post Graduation Student with great attention to detail and analytical skills.

Python In Plain English

Go deeper with the language powering everything.

Vinodnethichinna

Written by

Technical, Enthusiastic and Organized Post Graduation Student with great attention to detail and analytical skills.

Python In Plain English

Go deeper with the language powering everything.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store