Thursday, May 7, 2020

IBM Watson Studio: Download pandas DataFrame as CSV or Excel file

Gist code snippet
Right now, I am working with IBM Watson Studio on some analytics side project. It is nice to work with Jupyter Notebooks, Python and pandas and data again. Even Db2 is involved.

From working with pandas DataFrames locally, I knew how to turn the data into CSV or Excel files. But working with a hosted environment, accessing the file system is not possible and some other solution is needed.

 

 DataFrame to CSV and Excel file

In pandas, a DataFrame is nothing more like a regular (database) table. DataFrames offer many functions to transform or modify the data, including functions to turn the data into a CSV (to_csv()) or Excel file (to_excel()). The functions work well if you have access to the local file system of the Notebook or Python environment.

IBM Watson Studio offers hosted Jupyter Notebooks and hence direct access to the file system is not possible. Generated download links also do not work because of security and reverse proxies. Thus, after some research, encoding the CSV or Excel file as base64 data and embedding the data into the link, seems the way to go. Here is my code for reference:


 Calling the two wrapper functions generates download links as cell output:
Download link for DataFrame as CSV or Excel


If you have feedback, suggestions, or questions about this post, please reach out to me on Twitter (@data_henrik) or LinkedIn.