【问题标题】: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)
【解决方案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 的直接解决方案,但没有成功。如果有人有更好的答案,请告诉我。到目前为止,这有效。