python—DataFrame的存储

转载于:http://blog.sina.com.cn/s/blog_4ddef8f80102vu4e.html

Pandas中的DataFrame数据既可以存储在SQL数据库中,也可以直接存储在CSV文件中。

数据库

1. dataframe.to_sql()函数将DataFrame数据存储到数据库中。

name :数据表的名称, string, Name of SQL table

con : 数据库的链接,SQLAlchemy engine or DBAPI2 connection (legacy mode)

if_exists : 如果SQL表已经存在的处理方式,{‘fail’, ‘replace’, ‘append’}, default ‘fail’

​  取消存储, fail: If table exists, do nothing.

​  替换SQL表,replace: If table exists, drop it, recreate it, and insert data.

​  附加在SQL表后,append: If table exists, insert data. Create if does not exist.

index : 是否将行标签存入SQL表,boolean, default
True
Write DataFrame index as a column.

index_label : 如果存储行标签,指定该列的名称,string or sequence, default None, Column
label for index column(s). 

2. pandas.read_sql()函数将SQL表读入DataFrame。

sql : 要读取的SQL表,string, database table name.

con : 与数据库的链接,SQLAlchemy engine

index_col : 指定用于行标签的列,string, optional, column
name to use as index for the returned DataFrame object.

CSV文件

1. dataframe.to_csv()函数将DataFrame数据存储在指定的csv文件中。该函数常用的参数有

columns : 指定需要存储的列,Columns to write

header : 是否把列的名字写入CSV文件, 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 : 是否把index写入CSV文件,boolean, default True.Write row labels (index)

index_label : 如果将index写入CSV文件,那么给出index列的标签(列的名字),string or sequence, or False, default None. 

2. pandas.read_csv()函数将CSV文件中的数据读入DataFrame。

header : CSV文件的列名所在的行, 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 : 指定行标签所在的列,int or sequence or False, default None. Column to use as the row labels of the DataFrame. 

names : 列的名称,array-like. List of column names to use. 

总结:

相同点:​

1. 存储时,CSV和SQL都默认存储行标签(index),因此需要指定存储行标签列的名字,index_label。

2. 读取时,CSV和SQL都需要指定哪一列是行标签(index)列,index_col,不同的是CSV是通过列的序号指定,SQL是通过列的名字指定。​

不同点:

1. SQL一定会存储列标签,CSV可以存储列标签也可以不存储列标签。​

转载自:https://blog.csdn.net/u012131430/article/details/78299434

You may also like...

退出移动版