Pandas中的DataFrame数据既可以存储在SQL数据库中,也可以直接存储在CSV文件中。
1. dataframe.to_sql()函数将DataFrame数据存储到数据库中。
name :数据表的名称,string, Nameof SQL table
con : 数据库的链接,SQLAlchemyengine or DBAPI2 connection (legacy mode)
if_exists :如果SQL表已经存在的处理方式,{‘fail’, ‘replace’, ‘append’}, default‘fail’
取消存储, fail: If table exists, donothing.
替换SQL表,replace: If table exists, drop it,recreate it, and insert data.
附加在SQL表后,append: If table exists, insertdata. Create if does not exist.
index :是否将行标签存入SQL表,boolean, default True, Write DataFrame index as acolumn.
index_label :如果存储行标签,指定该列的名称,string or sequence, defaultNone, Column label for indexcolumn(s).
2.pandas.read_sql()函数将SQL表读入DataFrame。
sql :要读取的SQL表,string, database table name.
con : 与数据库的链接,SQLAlchemyengine
index_col :指定用于行标签的列,string, optional, column name to use as index for thereturned DataFrame object.
CSV文件
1. dataframe.to_csv()函数将DataFrame数据存储在指定的csv文件中。该函数常用的参数有
columns : 指定需要存储的列,Columnsto write
header :是否把列的名字写入CSV文件, boolean, default True. Write outcolumn names. If a list of string is given it is assumed to bealiases for the column names
index :是否把index写入CSV文件,boolean, default True.Write rowlabels (index)
index_label :如果将index写入CSV文件,那么给出index列的标签(列的名字),string or sequence, or False,default None.
2.pandas.read_csv()函数将CSV文件中的数据读入DataFrame。
header: CSV文件的列名所在的行, int, list ofints. Row number(s) to use as the column names,and the start of the data. Defaults to0 if no names passed,otherwise None.
index_col :指定行标签所在的列,int or sequence or False, defaultNone. Column to use as the row labels of theDataFrame.
names :列的名称,array-like. List of column names touse.
总结:
相同点:?
1.存储时,CSV和SQL都默认存储行标签(index),因此需要指定存储行标签列的名字,index_label。
2.读取时,CSV和SQL都需要指定哪一列是行标签(index)列,index_col,不同的是CSV是通过列的序号指定,SQL是通过列的名字指定。?
不同点:
1.SQL一定会存储列标签,CSV可以存储列标签也可以不存储列标签。?
联系客服