如何从Cloud Datalab将数据框导出到BigQuery表?

时间:2015-12-17 13:37:43

标签: python google-bigquery google-cloud-datalab

我正在使用Google Cloud Datalab,我想将Pandas数据框导出为新的BigQuery表。我正在尝试使用Cloud Datalab附带的在线帮助笔记本,但我可以看到没有导出到BigQuery的示例,只有Google Cloud Storage。

无论如何,我可以弄清楚如何使用正确的架构在BigQuery中创建表,但我无法弄清楚如何将实际数据放入表中!

这就是我现在所拥有的:

dataset = bq.DataSet('calculations')
dataset.create(friendly_name='blah', 
               description='blah blah')
print 'Dataset exists', dataset.exists()

# Create the schema for the table we're about to create.
schema = bq.Schema.from_dataframe(measures[0]['data'])
print schema
print len(measures[0]['data'])

# Create a table for our results.
temptable = bq.Table('calculations.test').create(schema=schema, 
                                                 overwrite=True)

# How to export the actual data to the table?

所以输出:

True
[{'type': 'STRING', 'name': u'id'}, {'type': 'STRING', 'name': ...
8173

显示我的数据框有8173行。

如果我转到BigQuery,我看到该表是使用正确的模式创建的,但它没有数据。

如何在那里实际导出数据?

如果不可能,那么我可以导出到云存储,但我已经尝试过并且遇到了同样的问题。我宁愿导出到BigQuery。

1 个答案:

答案 0 :(得分:2)

您需要致电:

temptable.insert_data(df)

其中df是您的Pandas数据帧。

相关问题