Upload Pandas data frame from local machine to Google Cloud bucket(将 pandas 数据帧从本地机器上传到Google Cloud Bucket)
本文介绍了将 pandas 数据帧从本地机器上传到Google Cloud Bucket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想直接从本地机器上传 pandas 数据框到Google云存储,所以我不在云函数中。我使用write-a-pandas-dataframe-to-google-cloud-storage-or-bigquery尝试了不同的方法。但我无法保存。
注意:我只能使用google.cloud包
下面是我尝试的代码
from google.cloud import storage
import pandas as pd
input_dict = [{'Name': 'A', 'Id': 100}, {'Name': 'B', 'Id': 110}, {'Name': 'C', 'Id': 120}]
df = pd.DataFrame(input_dict)
尝试:1
destination = f'gs://bucket_name/test.csv'
df.to_csv(destination)
尝试:2
storage_client = storage.Client(project='project')
bucket = storage_client.get_bucket('bucket_name')
gs_file = bucket.blob('test.csv')
df.to_csv(gs_file)
我正在显示以下错误
对于选项1:没有这样的文件或目录:‘gs://bucket_name/test.csv’
选项2:‘blob’对象没有‘Close’属性
谢谢,
Raghunath。
推荐答案
from google.cloud import storage
import os
from io import StringIO # if going with no saving csv file
# say where your private key to google cloud exists
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/your-google-cloud-private-key.json'
df = pd.DataFrame([{'Name': 'A', 'Id': 100}, {'Name': 'B', 'Id': 110}])
先将其写入您计算机上的CSV文件,然后上传:
df.to_csv('local_file.csv')
gcs.get_bucket('BUCKET_NAME').blob('FILE_NAME.csv').upload_from_filename('local_file.csv', content_type='text/csv')
如果不想创建临时CSV文件,请使用StringIO:
f = StringIO()
df.to_csv(f)
f.seek(0)
gcs.get_bucket('BUCKET_NAME').blob('FILE_NAME.csv').upload_from_file(f, content_type='text/csv')
这篇关于将 pandas 数据帧从本地机器上传到Google Cloud Bucket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:将 pandas 数据帧从本地机器上传到Google Cloud Bucket


基础教程推荐
猜你喜欢
- 将 YAML 文件转换为 python dict 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01