Last active
July 19, 2021 07:57
-
-
Save jmquintana79/30462b0f2b60107742c394fa116fbd41 to your computer and use it in GitHub Desktop.
Store Pandas dataframe content into MongoDb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pymongo import MongoClient | |
from odo import odo | |
import pandas as pd | |
# open connection | |
connection = MongoClient() | |
# pandas df creation | |
DF = pd.DataFrame({'A': [1,2,3,4,5,6,7], 'B':[10,20,30,40,50,60,70]}) | |
# database connection | |
db = connection.<database> | |
# insert df into mongo <table> | |
odo(DF, db.<table>) |
Thanks for the response. I found a solution that worked out for me:
To save:
mongo_dict = json.loads(df.T.to_json())
mongo.db.collection_name.save(mongo_dict)
To load:
mongo_dict = mongo.db.collection_name.find_one({"_id": id})
df= pd.DataFrame.from_dict(mongo_dict, orient='index')
Thank you very much. Interesting.
data='some example dataframe'
data.reset_index(inplace=True)
data_dict = data.to_dict("records")
# Insert collection
collection.insert_many(data_dict)
So simple. Thank you very much.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry because answering so late. I didn't know about that. When I created my snippet I tested it with a previous version of Pandas (I cannot remember which one) and it worked. If I am honest, I don't have used odo library yet. Maybe I should check it and/or at least including a list of requeriments with libraries and their respective versions. Thanks