|
Pandas中的DataFrame數(shù)據(jù)既可以存儲在SQL數(shù)據(jù)庫中,也可以直接存儲在CSV文件中。 數(shù)據(jù)庫1. dataframe.to_sql()函數(shù)將DataFrame數(shù)據(jù)存儲到數(shù)據(jù)庫中。 name :數(shù)據(jù)表的名稱, string, Name of SQL table con : 數(shù)據(jù)庫的鏈接,SQLAlchemy engine or DBAPI2 connection (legacy mode)
index_label : 如果存儲行標簽,指定該列的名稱,string or sequence, default None, Column label for index column(s). 2. pandas.read_sql()函數(shù)將SQL表讀入DataFrame。 sql : 要讀取的SQL表,string, database table name. con : 與數(shù)據(jù)庫的鏈接,SQLAlchemy engine index_col : 指定用于行標簽的列,string, optional, column name to use as index for the returned DataFrame object. CSV文件 1. dataframe.to_csv()函數(shù)將DataFrame數(shù)據(jù)存儲在指定的csv文件中。該函數(shù)常用的參數(shù)有 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()函數(shù)將CSV文件中的數(shù)據(jù)讀入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. 總結(jié): 相同點:? 1. 存儲時,CSV和SQL都默認存儲行標簽(index),因此需要指定存儲行標簽列的名字,index_label。 2. 讀取時,CSV和SQL都需要指定哪一列是行標簽(index)列,index_col,不同的是CSV是通過列的序號指定,SQL是通過列的名字指定。? 不同點: 1. SQL一定會存儲列標簽,CSV可以存儲列標簽也可以不存儲列標簽。? |
|
|
來自: powerbaby > 《DataFrame》