Converting datasets to the dataframe is the best way to manipulate it. In this tutorial you will know how to first export dataframe to JSON and then import or insert the JSON into MongoDB database.
Part 1 : Export dataframe to JSON
Lets know all the steps you will follow to convert or export dataframe to JSON file.
Step 1: Create sample dataframe
Lets create a sample dataframe that will be converted to
import pandas as pd
data = {
'Name': ['Sukesh', 'Abhsihek', 'Maya', 'Rob'],
'Age': [25, 26, 35, 40],
'City': ['Delhi', 'Delhi', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
print("Sample DataFrame:")
print(df)
Step 2: Export the dataframe to JSON
After the creation of the dataframe you will use the dataframe.to_json() function to export the datatframe to JSON. Use the below line of code to export the dataframe to JSON file.
df.to_json('data.json', orient='records')
[
{
"Name": "Sukesh",
"Age": 25,
"City": "Delhi"
},
{
"Name": "Abhsihek",
"Age": 26,
"City": "Delhi"
},
{
"Name": "Maya",
"Age": 35,
"City": "Los Angeles"
},
{
"Name": "Rob",
"Age": 40,
"City": "Chicago"
}
]
Part 2: Inserting the dataframe to Mongodb
In the part one you have converted the dataframe to JSON file. In the second part you will import or insert the dataframe to MongoDB using pymongo module.
Pymongo is a MongoDB driver that allows you perform SQL opearations on MongoDB database. You can install the pymongo module using the pip command.
For python 2.xx
pip install pymongo
For python 3.xx
pip3 install pymongo
Lets know all the steps to import the dataframe to MongoDB
Step 1: Import all the required libraries
To import JSON to MongoDB you will required to import all the necessary libraries. Here I am using only json and pymongo module. So lets import it using the import statement.
from pymongo import MongoClient
import json
Step 2: Connect to the MongoDB
Now the next step is to connect the database to insert the JSON recods to the database. You will first create client for connection using the MongoClient() function then db and then collection name. Use the below lines of code for that.
client = MongoClient('mongodb://localhost:27017/')
db = client['your_database_name']
collection = db['your_collection_name']
Step 3: Read the JSON file
After making the connection you will read the JSON file that you have saved already with the open() function of filestream f.
with open('data.json') as f:
data = json.load(f)
Step 4: Insert the JSON to the Mongodb
The last step is to insert the JSON response to your database using the collection.inser_many() function. This function will accept JSON data as the parameter.
collection.insert_many(data)
Below is the full code for the entire opeartion above.
from pymongo import MongoClient
import json
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['your_database_name']
collection = db['your_collection_name']
# Read JSON file
with open('data.json') as f:
data = json.load(f)
# Insert data into MongoDB
collection.insert_many(data)
Thats all the steps you have to follow to convert the dataframe to the JSON and then import the JSON to the MongoDB database.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.