Python accesses MongoDB and converts to Dataframe

Source: Internet
Author: User
Tags mongoclient mongodb pycharm community edition

#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2018/7/13 11:10#@Author: Baoshan#@Site:#@File: pandans_pymongo.py#@Software: pycharm Community EditionImportPymongoImportPandas as PDdef_connect_mongo (host, port, username, password, db):"""a util for making a connection to MONGO."""    ifUsername andPassword:mongo_uri="Mongodb://%s:%[email protected]%s:%s/%s"%(username, password, host, port, DB) Conn=Pymongo. Mongoclient (Mongo_uri)Else: Conn=Pymongo. Mongoclient (host, Port)returnConn[db]defRead_mongo (DB, Collection, query={}, host='test43', port=27017, Username=none, Password=none, no_id=True):"""Read from Mongo and Store into DataFrame."""    #Connect to MongoDBdb = _connect_mongo (Host=host, Port=port, Username=username, Password=password, db=db)#Make a query to the specific DB and Collectioncursor = db[collection].find (query). Limit (10)    #Expand the cursor and construct the DataFrameDF =PD. DataFrame (cursor) df.to_csv ("Abc.csv", encoding="Utf_8_sig")#handling Chinese garbled problems    ifno_id:deldf['_id']    returnDfread_mongo (DB='Service', collection='Trace_log_regular', query={}, host='XXX', port=27017, Username="XXX", password="XXX")

Summary:

1. Fixed Pymongo access to MongoDB

2. Solve the problem of turning query data into Dataframe

3. Resolves an issue with dataframe writing to CSV

4. Solved the problem of Chinese garbled characters.

Thank you

"Reference" Https://stackoverflow.com/questions/16249736/how-to-import-data-from-mongodb-to-pandas

Python accesses MongoDB and converts to Dataframe

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.