<bdo id='nGB0Z'></bdo><ul id='nGB0Z'></ul>
  1. <legend id='nGB0Z'><style id='nGB0Z'><dir id='nGB0Z'><q id='nGB0Z'></q></dir></style></legend>
  2. <small id='nGB0Z'></small><noframes id='nGB0Z'>

    <i id='nGB0Z'><tr id='nGB0Z'><dt id='nGB0Z'><q id='nGB0Z'><span id='nGB0Z'><b id='nGB0Z'><form id='nGB0Z'><ins id='nGB0Z'></ins><ul id='nGB0Z'></ul><sub id='nGB0Z'></sub></form><legend id='nGB0Z'></legend><bdo id='nGB0Z'><pre id='nGB0Z'><center id='nGB0Z'></center></pre></bdo></b><th id='nGB0Z'></th></span></q></dt></tr></i><div id='nGB0Z'><tfoot id='nGB0Z'></tfoot><dl id='nGB0Z'><fieldset id='nGB0Z'></fieldset></dl></div>

    <tfoot id='nGB0Z'></tfoot>

      将 pandas 数据帧从本地机器上传到Google Cloud Bucket

      Upload Pandas data frame from local machine to Google Cloud bucket(将 pandas 数据帧从本地机器上传到Google Cloud Bucket)
        <bdo id='I9yAE'></bdo><ul id='I9yAE'></ul>

            <tfoot id='I9yAE'></tfoot>

            <legend id='I9yAE'><style id='I9yAE'><dir id='I9yAE'><q id='I9yAE'></q></dir></style></legend>

            <small id='I9yAE'></small><noframes id='I9yAE'>

                <i id='I9yAE'><tr id='I9yAE'><dt id='I9yAE'><q id='I9yAE'><span id='I9yAE'><b id='I9yAE'><form id='I9yAE'><ins id='I9yAE'></ins><ul id='I9yAE'></ul><sub id='I9yAE'></sub></form><legend id='I9yAE'></legend><bdo id='I9yAE'><pre id='I9yAE'><center id='I9yAE'></center></pre></bdo></b><th id='I9yAE'></th></span></q></dt></tr></i><div id='I9yAE'><tfoot id='I9yAE'></tfoot><dl id='I9yAE'><fieldset id='I9yAE'></fieldset></dl></div>
                  <tbody id='I9yAE'></tbody>
              1. 本文介绍了将 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                How to break out of multiple loops?(如何打破多个循环?)
                How to put the legend out of the plot(如何将传说从情节中剔除)
                Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                1. <i id='PTPSK'><tr id='PTPSK'><dt id='PTPSK'><q id='PTPSK'><span id='PTPSK'><b id='PTPSK'><form id='PTPSK'><ins id='PTPSK'></ins><ul id='PTPSK'></ul><sub id='PTPSK'></sub></form><legend id='PTPSK'></legend><bdo id='PTPSK'><pre id='PTPSK'><center id='PTPSK'></center></pre></bdo></b><th id='PTPSK'></th></span></q></dt></tr></i><div id='PTPSK'><tfoot id='PTPSK'></tfoot><dl id='PTPSK'><fieldset id='PTPSK'></fieldset></dl></div>
                  <tfoot id='PTPSK'></tfoot>

                  <small id='PTPSK'></small><noframes id='PTPSK'>

                    <tbody id='PTPSK'></tbody>
                  <legend id='PTPSK'><style id='PTPSK'><dir id='PTPSK'><q id='PTPSK'></q></dir></style></legend>

                        <bdo id='PTPSK'></bdo><ul id='PTPSK'></ul>