我正在尝试使用以下代码行将DataFrame(df
)上传到云存储中:
bucket.blob(gcs_reference).upload_from_string(df.to_csv(index=False, encoding='utf-8'), content_type=''application/octet-stream')
但是,我的DataFrame对于某些单元格没有值,因此会触发错误:
ValueError:无法将任何内容都不转换为Unicode
是否有办法抑制此情况,并为None放空值?过去我对此没有任何问题,我不记得自己做过什么。
答案 0 :(得分:1)
我能够复制,并且对我有用:
import pandas as pd
from gcloud import storage
client = storage.Client()
bucket = client.get_bucket('source')
d = {'col1': [1, 2], 'col2': [3, None]}
df = pd.DataFrame(data=d)
bucket.blob('file').upload_from_string(df.to_csv(index=False, encoding='utf-8'),
content_type='application/octet-stream')
文件内容为:
col1,col2
1,3.0
2,
或者您可以使用:
df.fillna(value=0, inplace=True)
bucket.blob('file').upload_from_string(df.to_csv(index=False,
encoding='utf-8'), content_type='application/octet-stream')
输出文件将是:
col1,col2
1,3.0
2,0.0
您的代码中包含以下部分:
content_type=''application/octet-stream'
这是语法错误
content_type =''应用程序/八位字节流''