Save Dataframe to csv directly to s3 Python

前端 未结 10 919
独厮守ぢ
独厮守ぢ 2020-11-28 02:02

I have a pandas DataFrame that I want to upload to a new CSV file. The problem is that I don\'t want to save the file locally before transferring it to s3. Is there any meth

10条回答
  •  無奈伤痛
    2020-11-28 02:44

    You can use:

    from io import StringIO # python3; python2: BytesIO 
    import boto3
    
    bucket = 'my_bucket_name' # already created on S3
    csv_buffer = StringIO()
    df.to_csv(csv_buffer)
    s3_resource = boto3.resource('s3')
    s3_resource.Object(bucket, 'df.csv').put(Body=csv_buffer.getvalue())
    

提交回复
热议问题