Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "_id" : ObjectId("5ad0ade0bef1fc2fba99489d"),
- "property_a" : 0.0,
- "property_b" : 0.0,
- "property_c" : 0.0,
- "property_d" : 0.0,
- "property_e" : 0.0,
- .....
- }
- import pandas as pd
- from pymongo import MongoClient
- def _connect_mongo(host, port, username, password, db):
- """ A util for making a connection to mongo """
- if username and password:
- mongo_uri = 'mongodb://%s:%s@%s:%s/%s' % (username, password, host, port, db)
- conn = MongoClient(mongo_uri)
- else:
- conn = MongoClient(host, port)
- return conn[db]
- def read_mongo(db, collection, query={}, host='localhost', port=27017, username=None, password=None, no_id=True):
- """ Read from Mongo and Store into DataFrame """
- # Connect to MongoDB
- db = _connect_mongo(host=host, port=port, username=username, password=password, db=db)
- # Make a query to the specific DB and Collection
- cursor = db[collection].find(query)
- # Expand the cursor and construct the DataFrame
- df = pd.DataFrame(list(cursor))
- # Delete the _id
- if no_id:
- del df['_id']
- return df
Add Comment
Please, Sign In to add comment