python – 将Pandas DataFrame写入Google Cloud Storage或BigQuery
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 将Pandas DataFrame写入Google Cloud Storage或BigQuery,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1844字,纯文字阅读大概需要3分钟。
内容图文
![python – 将Pandas DataFrame写入Google Cloud Storage或BigQuery](/upload/InfoBanner/zyjiaocheng/708/c0dff6735d5d4056bca2a85062cf8aa1.jpg)
您好,感谢您的时间和考虑.
我正在Google Cloud Platform / Datalab中开发一个Jupyter笔记本.
我创建了一个Pandas DataFrame,并希望将此DataFrame写入Google云端存储(GCS)和/或BigQuery.我在GCS中有一个存储桶,并通过以下代码创建了以下对象:
import gcp
import gcp.storage as storage
project = gcp.Context.default().project_id
bucket_name = 'steve-temp'
bucket_path = bucket_name
bucket = storage.Bucket(bucket_path)
bucket.exists()
我尝试过基于Google Datalab文档的各种方法,但仍然失败.
谢谢
解决方法:
尝试以下工作示例:
from datalab.context import Context
import google.datalab.storage as storage
import google.datalab.bigquery as bq
import pandas as pd
# Dataframe to write
simple_dataframe = pd.DataFrame(data=[{1,2,3},{4,5,6}],columns=['a','b','c'])
sample_bucket_name = Context.default().project_id + '-datalab-example'
sample_bucket_path = 'gs://' + sample_bucket_name
sample_bucket_object = sample_bucket_path + '/Hello.txt'
bigquery_dataset_name = 'TestDataSet'
bigquery_table_name = 'TestTable'
# Define storage bucket
sample_bucket = storage.Bucket(sample_bucket_name)
# Create storage bucket if it does not exist
if not sample_bucket.exists():
sample_bucket.create()
# Define BigQuery dataset and table
dataset = bq.Dataset(bigquery_dataset_name)
table = bq.Table(bigquery_dataset_name + '.' + bigquery_table_name)
# Create BigQuery dataset
if not dataset.exists():
dataset.create()
# Create or overwrite the existing table if it exists
table_schema = bq.Schema.from_data(simple_dataframe)
table.create(schema = table_schema, overwrite = True)
# Write the DataFrame to GCS (Google Cloud Storage)
%storage write --variable simple_dataframe --object $sample_bucket_object
# Write the DataFrame to a BigQuery table
table.insert(simple_dataframe)
我使用了this示例,并将datalab github site中的_table.py文件作为参考.您可以在this链接找到其他datalab源代码文件.
内容总结
以上是互联网集市为您收集整理的python – 将Pandas DataFrame写入Google Cloud Storage或BigQuery全部内容,希望文章能够帮你解决python – 将Pandas DataFrame写入Google Cloud Storage或BigQuery所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。