我试图从mongodb中读取json后将json转换为csv。要阅读Mongodb Collection我的代码是
import pymongo
url="mongodb://user_name:password@mlab.com:134/xyz"
client=pymongo.MongoClient(url)
db = client['xyz']
collection = db['Transaction']
我将Collection_data转换为json。我的代码是:
from bson.json_util import dumps
r = collection.find()
test=dumps(r)
json_test=json.loads(test)
json_test
输出:
[{'_id': {'$oid': '5c7c9a39259bfb472a6b42be'}, 'products': [{'amount': '285', '_id': '5bf69f5ca59f6202a7c95434', 'dmrp': '95', 'quantity': '3', 'mrp': '115'}, {'amount': '140', '_id': '5beaba4d1e748515b8558988', 'dmrp': '70', 'quantity': '2', 'mrp': '80'}, {'amount': '21.17', '_id': '5c0f4f194449714eddac9554', 'dmrp': '21.17', 'quantity': '1', 'mrp': '25'}], 'transaction_amount': '446.17', 'session_id': '5c7c99e4259bfb472a6b42ba', 'user_id': '5c66b2a9607eef129a295f7c' }
我想将其转换为pandas Dataframe,其列应为数量和数量,其外观应如下所示:
amount quantity
285 3
140 2
21.17 1
我尝试过的代码是:
df = pd.concat(map(lambda x: DataFrame(x[1]['products'], index=np.repeat(x[0], 3)), enumerate(json_test)))
但是我遇到错误:
ValueError: Shape of passed values is (5, 8), indices imply (5, 3)