#!/usr/bin/env python
# coding: utf-8
# This notebook does not use Tatoeba data. Instead, it provides some functions that you may want to use in other notebooks. You can check how to:
# - [Save your data into a file you can download](#write_csv)
# Run the following cell to be able to run the examples.
# In[ ]:
import pandas as pd
#
# # Write a dataframe into a file that can be retrieved
# It is very simple to write a dataframe to a CSV file.
#
# Suppose you have the following dataframe
# In[ ]:
df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
'mask': ['red', 'purple'],
'weapon': ['sai', 'bo staff']})
df
# You can use the `to_csv` function to write your dataframe to a CSV file. If you have some Python knowledge, you can check the [documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html). Otherwise, the following examples are the most used.
#
# After running the `to_csv` function, go back to the `Home` page. All files in the current folder are displayed there, so you should see your `Data_export.csv` file. If you want to download it, simply check the box on its left and click the `Download` button at the top of the list.
# ### 1. Write everything
# Simply specify the name of the file you want to create.
# In[ ]:
df.to_csv('Data_export.csv')
# This will give you the following content
# In[ ]:
get_ipython().system("cat 'Data_export.csv'")
# ### 2. Do not write the index
# The index of a dataframe is the first column on the left (in bold). In some situations, it is a useless information so you can ignore it when you export your data to a file. To do so, add the `index=False` parameter.
# In[ ]:
df.to_csv('Data_export.csv', index=False)
# This will give you the following content
# In[ ]:
get_ipython().system("cat 'Data_export.csv'")
# ### 3. Do not write the headers
# If you don't need the column headers, add the `header=None` parameter.
# In[ ]:
df.to_csv('Data_export.csv', index=False, header=None)
# This will give you the following content
# In[ ]:
get_ipython().system("cat 'Data_export.csv'")
# ### 4. Change the separator
# The default separator is the comma `,` (hence, CSV :) ). If you want to change it, you can use `sep=''`. Note: a tabulation is represented by `\t`.
# In[ ]:
df.to_csv('Data_export.csv', index=False, header=None, sep='\t')
# This will give you the following content
# In[ ]:
get_ipython().system("cat 'Data_export.csv'")