Download CSV from an iPython Notebook

后端 未结 6 1026
一个人的身影
一个人的身影 2020-12-02 23:17

I run an iPython Notebook server, and would like users to be able to download a pandas dataframe as a csv file so that they can use it in their own environment. There\'s no

6条回答
  •  暖寄归人
    2020-12-02 23:53

    How about using the FileLinks class from IPython? I use this to provide access to data directly from Jupyter notebooks. Assuming your data is in pandas dataframe p_df:

    from IPython.display import FileLink, FileLinks
    
    p_df.to_csv('/path/to/data.csv', index=False)
    p_df.to_excel('/path/to/data.xlsx', index=False)
    
    FileLinks('/path/to/')
    

    Run this as a notebook cell and the result will be a list of links to files downloadable directly from the notebook. '/path/to' needs to be accessible for the notebook user of course.

提交回复
热议问题