简体   繁体   English

PyMongo 查询给了我一个嵌套的 DataFrame

[英]PyMongo query gives me back a nested DataFrame

I perform an aggregation with on a very large dataset.我在一个非常大的数据集上执行聚合。 This is my code:这是我的代码:

pipeline = [{"$match": {"Name_md_group": "ZEITUNGEN"}}, {"$group": {"_id": {"Name_freq": "$Name_freq"}, "total": {"$sum": "$Cost"}}}]
result = pd.DataFrame(list(collection.aggregate(pipeline)))

Result:结果:

                                          _id         total
    0  {'Name_freq': 'WOECHENTLICH FUENFMAL'}  2.074940e+07
    1               {'Name_freq': 'SONSTIGE'}  2.284889e+07
    2           {'Name_freq': 'WOECHENTLICH'}  8.522535e+07
    3               {'Name_freq': 'TAEGLICH'}  3.700943e+07
    4  {'Name_freq': 'WOECHENTLICH SECHSMAL'}  1.489394e+09

Somehow I get back a nested object?我以某种方式取回了一个嵌套对象? Why is that and is there a way to get rid of it?为什么会这样,有没有办法摆脱它? The column name _id should be Name_freq .列名_id应该是Name_freq Can anyone help me?谁能帮我?

Put the expression directly into _id and add an extra $project stage.将表达式直接放入_id并添加一个额外的$project阶段。

pipeline = [
  { $match: { Name_md_group: "ZEITUNGEN" } },
  { $group: { _id: "$Name_freq", total: { $sum: "$Cost" } } },
  { $project: { Name_freq: "$_id", _id: 0, total: 1 }}
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM