gpt4 book ai didi

pandas - xlsx pandas 写入 s3(带标签)

转载 作者:行者123 更新时间:2023-12-04 02:54:41 24 4
gpt4 key购买 nike

我有一个项目,我需要将数据帧写入 s3 存储桶中的 xlsx。使用 pandas 从 s3 加载文件非常简单: df= pd.read_excel('s3://path/file.xlsx')

但是将文件写入 s3 给我带来了问题。

 import pandas as pd

# Create a Pandas dataframe from the data.
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('s3://path/', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()

FileNotFoundError: [Errno 2] No such file or directory: 's3://path'

那么我如何使用 pandas 将 xlsx 文件写入 s3,最好使用 tabs?

最佳答案

import io
import boto3
import xlsxwriter
import pandas as pd

bucket = 'your-s3-bucketname'
filepath = 'path/to/your/file.format'
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

with io.BytesIO() as output:
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df.to_excel(writer, 'sheet_name')
data = output.getvalue()
s3 = boto3.resource('s3')
s3.Bucket(bucket).put_object(Key=filepath, Body=data)

关于pandas - xlsx pandas 写入 s3(带标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53565895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com