{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "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:\n", "- [Save your data into a file you can download](#write_csv)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the following cell to be able to run the examples." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Write a dataframe into a file that can be retrieved" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is very simple to write a dataframe to a CSV file. \n", "\n", "Suppose you have the following dataframe" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame({'name': ['Raphael', 'Donatello'],\n", " 'mask': ['red', 'purple'],\n", " 'weapon': ['sai', 'bo staff']})\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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. \n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1. Write everything" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simply specify the name of the file you want to create." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_csv('Data_export.csv')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will give you the following content" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat 'Data_export.csv'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Do not write the index" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_csv('Data_export.csv', index=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will give you the following content" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat 'Data_export.csv'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Do not write the headers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don't need the column headers, add the `header=None` parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_csv('Data_export.csv', index=False, header=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will give you the following content" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat 'Data_export.csv'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Change the separator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df.to_csv('Data_export.csv', index=False, header=None, sep='\\t')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This will give you the following content" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!cat 'Data_export.csv'" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }