Save Dataframe to csv directly to s3 Python

前端 未结 10 928
独厮守ぢ
独厮守ぢ 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:31

    I like s3fs which lets you use s3 (almost) like a local filesystem.

    You can do this:

    import s3fs
    
    bytes_to_write = df.to_csv(None).encode()
    fs = s3fs.S3FileSystem(key=key, secret=secret)
    with fs.open('s3://bucket/path/to/file.csv', 'wb') as f:
        f.write(bytes_to_write)
    

    s3fs supports only rb and wb modes of opening the file, that's why I did this bytes_to_write stuff.

提交回复
热议问题