我从 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 吗?
请您参考如下方法:
我将尝试用 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']
希望对你有帮助。
