Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python mongo';s组结果给熊猫';s数据帧_Python_Mongodb_Pandas_Dataframe - Fatal编程技术网

Python mongo';s组结果给熊猫';s数据帧

Python mongo';s组结果给熊猫';s数据帧,python,mongodb,pandas,dataframe,Python,Mongodb,Pandas,Dataframe,是否有任何方便的方法将mongodb组操作的结果加载到数据帧中 以下是mongo查询的分组类型: '$group': { '_id': {'country': '$country', 'oem':'$oem'}, 'count': {'$sum': 1} } 这是一个字典列表,如下所示: [ ... { "_id" : { "country" : "US", "oem" : "mmm" }, "count" : 595 },

是否有任何方便的方法将mongodb组操作的结果加载到数据帧中

以下是mongo查询的分组类型:

    '$group': {
        '_id': {'country': '$country', 'oem':'$oem'},
         'count': {'$sum': 1}
    }
这是一个字典列表,如下所示:

[
   ...
   { "_id" : { "country" : "US", "oem" : "mmm" }, "count" : 595 },
   ...
]
我希望将其加载到数据框中,以便
country
oem
自动成为索引。除了重新映射结果,Pandas API中是否有任何东西可以处理这个问题?或者我可以以某种方式重新编写mongo查询,使其返回一个对pandas API更友好的结构吗?

您可以使用:

设置:

l = [
   { "_id" : { "country" : "UA", "oem" : "uuuu" }, "count" : 555 },
   { "_id" : { "country" : "US", "oem" : "aaaa" }, "count" : 595 },
   { "_id" : { "country" : "DE", "oem" : "bbbb" }, "count" : 777 },
]

您的预期输出是什么?一个包含三列的数据框:
country
oem
count
(可选按
country
oem
索引)
l = [
   { "_id" : { "country" : "UA", "oem" : "uuuu" }, "count" : 555 },
   { "_id" : { "country" : "US", "oem" : "aaaa" }, "count" : 595 },
   { "_id" : { "country" : "DE", "oem" : "bbbb" }, "count" : 777 },
]