国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python訪問MongoDB,并且轉(zhuǎn)換成Dataframe的方法

2020-02-15 23:12:09
字體:
供稿:網(wǎng)友

如下所示:

#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/7/13 11:10# @Author : baoshan# @Site : # @File : pandans_pymongo.py# @Software: PyCharm Community Editionimport pymongoimport pandas as pddef _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 = pymongo.MongoClient(mongo_uri) else:  conn = pymongo.MongoClient(host, port) return conn[db]def read_mongo(db, collection, query={}, host='test43', 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).limit(10) # Expand the cursor and construct the DataFrame df = pd.DataFrame(list(cursor)) df.to_csv("abc.csv", encoding="utf_8_sig") # 處理中文亂碼問題 if no_id:  del df['_id'] return dfread_mongo(db='service', collection='trace_log_regular', query={}, host='xxx', port=27017, username="xxx", password="xxx")

小結(jié):

1. 解決了pymongo訪問MongoDB的問題

2. 解決了查詢的數(shù)據(jù)轉(zhuǎn)成dataframe的問題

3. 解決了dataframe寫入到csv的問題

4. 解決了中文亂碼問題。

以上這篇Python訪問MongoDB,并且轉(zhuǎn)換成Dataframe的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林站長站。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 达拉特旗| 德令哈市| 三亚市| 左权县| 蓬莱市| 会同县| 乐业县| 蒲城县| 合作市| 宜良县| 三原县| 栾川县| 德保县| 垣曲县| 贺州市| 吉林省| 梓潼县| 光泽县| 华蓥市| 秦安县| 常州市| 和政县| 鹿泉市| 上犹县| 亚东县| 吴忠市| 尼木县| 漳浦县| 和林格尔县| 清徐县| 依安县| 香河县| 博客| 沙田区| 高唐县| 阿瓦提县| 界首市| 蒙城县| 二连浩特市| 沙洋县| 石泉县|