栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将Pandas DataFrame写入Google Cloud Storage或BigQuery

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将Pandas DataFrame写入Google Cloud Storage或BigQuery

请尝试以下工作示例:

from datalab.context import Contextimport google.datalab.storage as storageimport google.datalab.bigquery as bqimport pandas as pd# Dataframe to writesimple_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_namesample_bucket_object = sample_bucket_path + '/Hello.txt'bigquery_dataset_name = 'TestDataSet'bigquery_table_name = 'TestTable'# Define storage bucketsample_bucket = storage.Bucket(sample_bucket_name)# Create storage bucket if it does not existif not sample_bucket.exists():    sample_bucket.create()# Define BigQuery dataset and tabledataset = bq.Dataset(bigquery_dataset_name)table = bq.Table(bigquery_dataset_name + '.' + bigquery_table_name)# Create BigQuery datasetif not dataset.exists():    dataset.create()# Create or overwrite the existing table if it existstable_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 tabletable.insert(simple_dataframe)

我使用了这个示例,并将datalab
github站点
的_table.py文件作为参考。您可以在此链接中找到其他源代码文件。

datalab



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624229.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号