For a pandas data frame you can use df.to_csv('data.csv').
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
Answer from Cor on Stack OverflowExport a pandas dataframe directly to a local file
pandas - saving a dataframe to csv file (python) - Stack Overflow
pandas - Download CSV from an iPython Notebook - Stack Overflow
Writing to CSV with pandas
How do I export a Pandas DataFrame to CSV without the index?
How do I export a data frame to a CSV file in Jupyter Notebook?
What is the syntax for exporting a data frame to CSV using Python in Jupyter Notebook?
For a pandas data frame you can use df.to_csv('data.csv').
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
If you have data in pandas DataFrame then you can use .to_csv() function from pandas to export your data in CSV .
Here's how you can save data in desktop
df.to_csv("<path to desktop and filename>")
# If you just use file name then it will save CSV file in working directory.
# Example path : C:/Users/<>/Desktop/<file name>.csv
If you want to learn more about it you can visit official documentation.
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.
For not too large tables you can use the following code:
import base64
import pandas as pd
from IPython.display import HTML
def create_download_link( df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv()
b64 = base64.b64encode(csv.encode())
payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
return HTML(html)
df = pd.DataFrame(data = [[1,2],[3,4]], columns=['Col 1', 'Col 2'])
create_download_link(df)