[英]How do I export a dataframe to a BigQuery table from Cloud Datalab?
I'm working in Google Cloud Datalab and I want to export a Pandas dataframe as a new BigQuery table. 我正在Google Cloud Datalab中工作,我想将Pandas数据框导出为新的BigQuery表。 I'm trying to follow the online help notebooks that come with Cloud Datalab, though I can see are no examples of exporting to BigQuery, only to Google Cloud Storage.
我正在尝试遵循Cloud Datalab随附的在线帮助笔记本,尽管我看不到仅导出到Google Cloud Storage的示例,也没有导出到BigQuery的示例。
Anyway, I can work out how to create the table in BigQuery with the correct schema, but I can't work out how to get the actual data into the table! 无论如何,我可以解决如何使用正确的架构在BigQuery中创建表,但是无法解决如何将实际数据放入表中!
This is what I've got now: 这就是我现在得到的:
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?
So this outputs: 所以这输出:
True
[{'type': 'STRING', 'name': u'id'}, {'type': 'STRING', 'name': ...
8173
showing that my dataframe has 8173 rows. 显示我的数据框有8173行。
If I go to BigQuery, I see that the table has been created with the correct schema, but it has no data in it. 如果我转到BigQuery,则会看到该表是使用正确的架构创建的,但是其中没有数据。
How do I actually export the data there? 我实际上如何在那里导出数据?
If it's impossible then I could export to Cloud Storage instead, though I've tried that and am having the same problem. 如果不可能,那么我可以导出到Cloud Storage,尽管我已经尝试过并且遇到了同样的问题。 I would prefer to export to BigQuery.
我希望导出到BigQuery。
You need to call: 您需要致电:
temptable.insert_data(df)
where df is your Pandas dataframe. df是您的Pandas数据框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.