简体   繁体   English

如何将dataFrame csv/excel文件导出到特定目录

[英]How to export dataFrame csv/excel file to a specific directory

Databricks dataFrame数据块数据帧

I have a dataFrame that I would like to export the result to a specific path/directory.我有一个数据帧,我想将结果导出到特定的路径/目录。

By doing display(df) , you got a button that you can press that will give you a download off the result that you can open in excel.通过执行display(df) ,您可以获得一个可以按下的按钮,该按钮将为您下载可以在 excel 中打开的结果。

How can I do this by using python?我如何使用 python 来做到这一点? I am trying to make a job in databricks that run in a specific schedule, and I want it to run my entire notebook, and in the end make it export the final result and save it in a specific directory so I can go back in time and see some old result.我正在尝试在按特定计划运行的数据块中完成一项工作,我希望它运行我的整个笔记本,最后让它导出最终结果并将其保存在特定目录中,以便我可以及时返回并看到一些旧的结果。

You can export the data to a CSV format(Which can be opened in Excel). 您可以将数据导出为CSV格式(可以在Excel中打开)。 In order to do so you can convert the DataFrame to a Pandas DataFrame and then convert it to CSV format. 为此,您可以将DataFrame转换为Pandas DataFrame,然后将其转换为CSV格式。

df.toPandas().to_csv("<FILE_DESTINATION>/<FILE_NAME>")

From what I understand, you want to know how to export a pandas dataframe(csv/ excel) to a directory: 据我了解,您想知道如何将pandas dataframe(csv / excel)导出到目录:

You can use: 您可以使用:

import pandas
specific_dir = '/path_of_dir'

df.to_csv(specific_dir+'/mycsv.csv')
df.to_excel(specific_dir+'/myexcel.xlsx')

You can just pass the directory you want to export the dataframe into in the commands above. 您只需在上面的命令中传递要将数据帧导出到的目录即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM