将DynamoDB数据流传输到Pandas数据帧

4

我真的很难弄清楚如何将我的AWS Dynamodb数据库值移动到pandas dataframe中。我的数据并不是特别大(100,000行)。我将数据放入s3中,然后为了简单起见下载了它。我试图使用boto3和S3Fs将数据导入pandas frame,但没有成功,因此现在我只好在创建了一个与S3的管道之后直接下载文件。输出以以下格式呈现,但pd.read_JSON、pd.read_csv和pd.read_table似乎无法理解。

 {"id":{"s":"3115136104"},"created":{"s":"Wed Mar 25 15:15:35 +0000 2015"},"location":{"s":"Dover, Kent"},"description":{"s":"#TrafficandWeather information from the #PortofDover - follow for regular round the clock updates. NOTE: we are not always able to respond to queries"},"friends_count":{"n":"66"},"name":{"s":"Port of Dover Travel"},"URL":{"s":"doverport.co.uk/weather/"},"statuses_count":{"n":"11062"},"lang":{"s":"en"},"followers_count":{"n":"11517"}}

这显然是JSON格式的,但因为它将每个行项目分解成一个没有键的字典。我完全不知道如何让pandas读取这个文件。

我的问题是,最简单的方法是什么,可以将整个dynamodb表格导入Pandas中,假设有一些空值?请注意,我已经尝试了很多种方法,但是没有任何进展,所以我没有包含任何通用代码。

1个回答

3

我已经自己回答了这个问题。

columns = ['id', 'created', 'description', 'followers_count', 'friends_count', 'lang', 'location', 'name', 'statuses_count', 'URL']
df = pd.DataFrame()
with open(r'C:\dynamodb-in-s3-file-that-was-downloaded') as s3:
    for item in s3:
        newdf = pd.read_json(item)
        newdf.fillna(method='ffill', inplace=True)
        newdf = newdf.loc['s']
        df = df.append(newdf, ignore_index=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接