Python Pandas嵌套MongoDB

2024-09-30 20:19:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试阅读get a nested mongodb result into a pandas dataframe。

数据看起来像这样。

{
"_id" : ObjectId("5911b9cebb56c016794d45a4"),
"crawlat" : "2017-05-09 14:45",
"traffic" : [ 
    {
        "timestamp" : "1494338401",
        "organic" : 53
    }, 
    {
        "timestamp" : "1494342001",
        "organic" : 64
    }, 
    {
        "timestamp" : "1494345601",
        "organic" : 74
    }, 
    {
        "timestamp" : "1494349201",
        "organic" : 78
    }, 
    {
        "timestamp" : "1494352801",
        "organic" : 80
    }, 
    {
        "timestamp" : "1494356401",
        "organic" : 88
    }, 
    {
        "timestamp" : "1494360001",
        "organic" : 91
    }, 
    {
        "timestamp" : "1494363601",
        "organic" : 92
    }, 
    {
        "timestamp" : "1494367201",
        "organic" : 94
    }
]

}

traffic数组包含每个结果的48个条目。

我只对按数组顺序排列的“有机”值感兴趣。

我从

^{pr2}$

我用json规范化和

dfsitemap = dfsitemap['traffic'].apply(pd.Series)

结果是这样的

enter image description here

但我需要一张只有有机值的表格。我该怎么清理这个?


Tags: 数据iddataframepandasgetmongodb数组result
1条回答
网友
1楼 · 发布于 2024-09-30 20:19:00

您可以使用from_records构造函数创建数据帧,该构造函数允许您指定要包含或排除的列:

pd.DataFrame.from_records(sitemapsdata['traffic'], exclude=['timestamp'])

它给出了:

image

相关问题 更多 >