【问题标题】:use python's pandas to treat aws dynamodb data使用 python 的 pandas 处理 aws dynamodb 数据
【发布时间】:2020-04-29 17:14:55
【问题描述】:

我从 dynamodb 表中获取数据,使用 boto3 for python 2.7,我会使用 pandas 对数据进行分组和排序。

不幸的是,dynamodb 数据格式有点棘手。像这样:

data = [{
      u 'permaname': {
        u 'S': u 'facebook'
      },
      u 'uuid': {
        u 'S': u '4b873085-c995-4ce4-9325-cfc70fcd4040'
      },
      u 'tags': {
        u 'L': []
      },
      u 'type': {
        u 'S': u 'xxxxxx'
      },
      u 'createdOn': {
        u 'N': u '1502099627'
      },
      u 'source': {
        u 'S': u 'xxxxxxx'
      },
      u 'data': {
        u 'NULL': True
      },
      u 'crawler': {
        u 'S': u 'xxxxxxx'
      }
    }, {
      u 'permaname': {
        u 'S': u 'facebook'
      },
      u 'uuid': {
        u 'S': u '25381aef-a7db-4b79-b599-89fd060fcf73'
      },
      u 'tags': {
        u 'L': []
      },
      u 'type': {
        u 'S': u 'xxxxxxx'
      },
      u 'createdOn': {
        u 'N': u '1502096901'
      },
      u 'source': {
        u 'S': u 'xxxxxxx'
      },
      u 'data': {
        u 'NULL': True
      },
      u 'crawler': {
        u 'S': u 'xxxxxxx'
      }
    }]

要进行分组和排序,我必须创建一个 pandas 对象,但我不知道该怎么做。

这就是我正在尝试的方式:

obj = pandas.DataFrame(data)
print list(obj.sort_values(['createdOn'],ascending=False).groupby('source'))

如果我这样打印 obj:

print list(obj)

我有:

[u'crawler', u'createdOn', u'data', u'permaname', u'source', u'tags', u'type', u'uuid']

有人知道如何使用 dynamodb 数据创建 dataFrame obj 吗?

【问题讨论】:

  • 你可以使用 pandas.json_normalize

标签: python python-2.7 pandas amazon-dynamodb


【解决方案1】:

我将尝试在 Python 3 中回答。

data = [{
       'permaname': {
         'S':  'facebook'
      },
       'uuid': {
         'S':  '4b873085-c995-4ce4-9325-cfc70fcd4040'
      },
       'tags': {
         'L': []
      },
       'type': {
         'S':  'xxxxxx'
      },
       'createdOn': {
         'N':  '1502099627'
      },
       'source': {
         'S':  'xxxxxxx'
      },
       'data': {
         'NULL': True
      },
       'crawler': {
         'S':  'xxxxxxx'
      }
    }, {
       'permaname': {
         'S':  'facebook'
      },
       'uuid': {
         'S':  '25381aef-a7db-4b79-b599-89fd060fcf73'
      },
     'tags': {
         'L': []
      },
       'type': {
         'S':  'xxxxxxx'
      },
       'createdOn': {
         'N':  '1502096901'
      },
       'source': {
         'S':  'xxxxxxx'
      },
       'data': {
         'NULL': True
      },
       'crawler': {
         'S':  'xxxxxxx'
      }
    }]

按照之前的建议使用 dynamodb_json。

from dynamodb_json import json_util as json
obj = pd.DataFrame(json.loads(data))
obj

有输出:

    crawler     createdOn   data    permaname   source  tags    type    uuid
0   xxxxxxx     1502099627  None    facebook    xxxxxxx     []  xxxxxx  4b873085-c995-4ce4-9325-cfc70fcd4040
1   xxxxxxx     1502096901  None    facebook    xxxxxxx     []  xxxxxxx     25381aef-a7db-4b79-b599-89fd060fcf73

分组依据(我使用 max() 来聚合结果)

obj.sort_values(['createdOn'],ascending=False).groupby('source').max()

有输出

       crawler  createdOn   data    permaname   tags    type    uuid
source                          
xxxxxxx     xxxxxxx     1502099627  NaN     facebook    []  xxxxxxx     4b873085-c995-4ce4-9325-cfc70fcd4040

打印列表

print(list(obj))

输出:

[u'crawler', u'createdOn', u'data', u'permaname', u'source', u'tags', u'type', u'uuid']

希望对你有帮助。

【讨论】:

    【解决方案2】:

    要将 dynamodb json 转换为常规 json,您可以使用此库:

    https://github.com/Alonreznik/dynamodb-json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-16
      • 2022-01-23
      • 2017-02-24
      • 2014-01-12
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 2023-01-20
      相关资源
      最近更新 更多