mongodb取出json,利用python转成dataframe(dict-to-dataframe)
1、mongodb数据源结构:
2、输出结果:
3、python代码部分
import pandas as pdfrom pymongo import MongoClient#1. get data from mongodbclass extra_yunnan_hotel(object): def get_yunnan_hotel(self): client = MongoClient('192.168.1.XXX',27017) db=client.gaode_pois data2=db.gaode_pois_hotel_yunnan_extra_mid01.find({},{"_id":0,'name':1,'lng':1,'lat':1}).limit(10) #创建一个空的dataframe df = pd.DataFrame(columns = ["_id", "name", "lng", "lat"]) for x in data2: #dict转成dataframe,注意.T的运用 pd_data=pd.DataFrame.from_dict(x,orient='index').T #插入df,忽略索引 df=df.append(pd_data, ignore_index=True) df.to_csv('_id_name_lng_lat2.csv',sep='\t',encoding='utf-8')#执行start=extra_yunnan_hotel() start.get_yunnan_hotel()