DataFrame Storage

Source: Internet
Author: User
Keywords dataframe pandas dataframe dataframe storage
DataFrame data in Pandas can be stored either in an SQL database or directly in a CSV file.

database
1. The dataframe.to_sql() function stores DataFrame data in the database.

name: the name of the data table, string, Name of SQL table

con: link to the database, SQLAlchemy engine or DBAPI2 connection (legacy mode)

if_exists: if the SQL table already exists, {‘fail’, ‘replace’, ‘append’}, default ‘fail’

Cancel storage, fail: If table exists, do nothing.

Replace SQL table, replace: If table exists, drop it, recreate it, and insert data.

Attached to the SQL table, append: If table exists, insert data. Create if does not exist.

Index: Whether to store the row label in the SQL table, boolean, default True, Write DataFrame index as a column.

index_label: If storing a row label, specify the name of the column, string or sequence, default None, Column label for index column(s).

2. The pandas.read_sql() function reads the SQL table into the DataFrame.

Sql: SQL table to be read, string, database table name.

con: link to the database, SQLAlchemy engine

index_col: Specify the column used for the row label, string, optional, column name to use as index for the returned DataFrame object.

CSV file
1. The dataframe.to_csv() function stores DataFrame data in the specified csv file. The commonly used parameters of this function are

columns: Specify the columns to be stored, Columns to write

header: Whether to write the column names to the CSV file, boolean, default True. Write out column names. If a list of string is given it is assumed to be aliases for the column names

index: Whether to write the index into the CSV file, boolean, default True.Write row labels (index)

index_label: If index is written to a CSV file, then give the index column label (column name), string or sequence, or False, default None.

2. The pandas.read_csv() function reads the data in the CSV file into the DataFrame.
header: The row of the column name of the CSV file, int, list of ints. Row number(s) to use as the column names, and the start of the data. Defaults to 0 if no names passed, otherwise None.

index_col: Specify the column where the row label is located, int or sequence or False, default None. Column to use as the row labels of the DataFrame.

names: Column names, array-like. List of column names to use.

to sum up:

Similarities:

1. When storing, both CSV and SQL store row labels (index) by default, so you need to specify the name of the row label column, index_label.

2. When reading, both CSV and SQL need to specify which column is the row label (index) column, index_col, the difference is that CSV is specified by the column serial number, and SQL is specified by the column name.

difference:

1. SQL will always store column labels. CSV can store column labels or not.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.