我已将excel文件导入到pandas数据框中,并完成了数据探索和清理过程。
我现在想将已清理的数据帧写入csv文件回Azure DataLake,而不首先将其保存为本地文件。我正在使用熊猫3。
我的代码如下所示:
token = lib.auth(tenant_id = '',
client_secret ='',
client_id = '')
adl = core.AzureDLFileSystem(token, store_name)
with adl.open(path='Raw/Gold/Myfile.csv', mode='wb') as f:
**in_xls.to_csv(f, encoding='utf-8')**
f.close()
我在粗体声明中得到以下转储。
TypeError:需要类似字节的对象,而不是'str'
我也试过但没有运气
with adl.open(path='Raw/Gold/Myfile.csv', mode='wb') as f:
with io.BytesIO(in_xls) as byte_buf:
byte_buf.to_csv(f, encoding='utf-8')
f.close()
我收到以下错误:
TypeError:需要类似字节的对象,而不是'DataFrame'
非常感谢任何想法/提示
答案 0 :(得分:4)
前几天我用python 3.X与熊猫一起工作了。此代码在内部部署计算机上运行,并连接到云中的azure数据存储。
假设df是一个pandas数据帧,您可以使用以下代码:
adl = core.AzureDLFileSystem(token, store_name='YOUR_ADLS_STORE_NAME')
#toke is your login token that was created by whatever ADLS login method you decided.
#Personally I use the ServiceProvider login
df_str = df.to_csv()
with adl.open('/path/to/file/on/adls/newfile.csv', 'wb') as f:
f.write(str.encode(df_str))
f.close()
此键将数据帧转换为字符串,而不是使用str.encode()函数。
希望这有帮助。