【问题标题】:Unable to write pandas Dataframes to csv, getting IO error无法将 pandas 数据帧写入 csv,出现 IO 错误
【发布时间】:2018-12-17 20:29:03
【问题描述】:

我有一个 pandas Df,我正在尝试写入 csv 但无法写入。这是我的代码:

_bytes = c_df.to_csv(None).encode()
with fs.open("s3://bucket/key1/_df.csv", 'wb') as f:
        f.write(_bytes)

【问题讨论】:

  • 您能粘贴代码生成的完整错误吗?

标签: python pandas export-to-csv


【解决方案1】:

您是否尝试过使用熊猫:

c_df.to_csv('s3://bucket/key1/_df.csv', index=False, header=True)

【讨论】:

  • 它说'str'对象没有属性'to_csv'
【解决方案2】:

这对我有帮助:

c_df.to_csv('_df.csv', index=False)  # Preserves the header if present

然后使用boto3分别上传到s3:

s3 = boto3.resource('s3')
s3.meta.client.upload_file(closed_aoi_filename, s3_bucket, os.path.join(key, closed_aoi_filename))

我尝试了将 pandas df 上传到 s3 的直接解决方案,但没有成功。如果有人有更好的答案,请告诉我。到目前为止,这有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 2023-03-28
    • 2017-10-30
    • 2022-01-03
    • 2015-09-28
    • 2017-11-08
    • 2021-01-18
    • 2023-02-21
    相关资源
    最近更新 更多