pandas中to_csv()和read_csv()参数详解
pandas.read_csv參數整理
讀取CSV(逗號分割)文件到DataFrame 也支持文件的部分導入和選擇迭代 更多幫助參見:http://pandas.pydata.org/pandas-docs/stable/io.html 參數: filepath_or_buffer?: str,pathlib。str, pathlib.Path, py._path.local.LocalPath or any object with a read() method (such as a file handle or StringIO) 可以是URL,可用URL類型包括:http, ftp, s3和文件。對于多文件正在準備中 本地文件讀取實例:://localhost/path/to/table.csv sep?: str, default ‘,’ 指定分隔符。如果不指定參數,則會嘗試使用逗號分隔。分隔符長于一個字符并且不是‘\s+’,將使用python的語法分析器。并且忽略數據中的逗號。正則表達式例子:'\r\t' delimiter?: str, default?None 定界符,備選分隔符(如果指定該參數,則sep參數失效) delim_whitespace?: boolean, default False. 指定空格(例如’ ‘或者’ ‘)是否作為分隔符使用,等效于設定sep='\s+'。如果這個參數設定為Ture那么delimiter 參數失效。 在新版本0.18.1支持 header?: int or list of ints, default ‘infer’ 指定行數用來作為列名,數據開始行數。如果文件中沒有列名,則默認為0,否則設置為None。如果明確設定header=0 就會替換掉原來存在列名。header參數可以是一個list例如:[0,1,3],這個list表示將文件中的這些行作為列標題(意味著每一列有多個標題),介于中間的行將被忽略掉(例如本例中的2;本例中的數據1,2,4行將被作為多級標題出現,第3行數據將被丟棄,dataframe的數據從第5行開始。)。 注意:如果skip_blank_lines=True 那么header參數忽略注釋行和空行,所以header=0表示第一行數據而不是文件的第一行。 names?: array-like, default None 用于結果的列名列表,如果數據文件中沒有列標題行,就需要執行header=None。默認列表中不能出現重復,除非設定參數mangle_dupe_cols=True。 index_col?: int or sequence or False, default None 用作行索引的列編號或者列名,如果給定一個序列則有多個行索引。 如果文件不規則,行尾有分隔符,則可以設定index_col=False 來是的pandas不適用第一列作為行索引。 usecols?: array-like, default None 返回一個數據子集,該列表中的值必須可以對應到文件中的位置(數字可以對應到指定的列)或者是字符傳為文件中的列名。例如:usecols有效參數可能是 [0,1,2]或者是 [‘foo’, ‘bar’, ‘baz’]。使用這個參數可以加快加載速度并降低內存消耗。 as_recarray?: boolean, default False 不贊成使用:該參數會在未來版本移除。請使用pd.read_csv(...).to_records()替代。 返回一個Numpy的recarray來替代DataFrame。如果該參數設定為True。將會優先squeeze參數使用。并且行索引將不再可用,索引列也將被忽略。 squeeze?: boolean, default False 如果文件值包含一列,則返回一個Series prefix?: str, default None 在沒有列標題時,給列添加前綴。例如:添加‘X’ 成為 X0, X1, ... mangle_dupe_cols?: boolean, default True 重復的列,將‘X’...’X’表示為‘X.0’...’X.N’。如果設定為false則會將所有重名列覆蓋。 dtype?: Type name or dict of column -> type, default None 每列數據的數據類型。例如 {‘a’: np.float64, ‘b’: np.int32} engine?: {‘c’, ‘python’}, optional Parser engine to use. The C engine is faster while the python engine is currently more feature-complete. 使用的分析引擎。可以選擇C或者是python。C引擎快但是Python引擎功能更加完備。 converters?: dict, default None 列轉換函數的字典。key可以是列名或者列的序號。 true_values?: list, default None Values to consider as True false_values?: list, default None Values to consider as False skipinitialspace?: boolean, default False 忽略分隔符后的空白(默認為False,即不忽略). skiprows?: list-like or integer, default None 需要忽略的行數(從文件開始處算起),或需要跳過的行號列表(從0開始)。 skipfooter?: int, default 0 從文件尾部開始忽略。 (c引擎不支持) skip_footer?: int, default 0 不推薦使用:建議使用skipfooter?,功能一樣。 nrows?: int, default None 需要讀取的行數(從文件頭開始算起)。 na_values?: scalar, str, list-like, or dict, default None 一組用于替換NA/NaN的值。如果傳參,需要制定特定列的空值。默認為‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘nan’`. keep_default_na?: bool, default True 如果指定na_values參數,并且keep_default_na=False,那么默認的NaN將被覆蓋,否則添加。 na_filter?: boolean, default True 是否檢查丟失值(空字符串或者是空值)。對于大文件來說數據集中沒有空值,設定na_filter=False可以提升讀取速度。 verbose?: boolean, default False 是否打印各種解析器的輸出信息,例如:“非數值列中缺失值的數量”等。 skip_blank_lines?: boolean, default True 如果為True,則跳過空行;否則記為NaN。 parse_dates?: boolean or list of ints or names or list of lists or dict, default False- boolean. True -> 解析索引
- list of ints or names. e.g. If [1, 2, 3] -> 解析1,2,3列的值作為獨立的日期列;
- list of lists. e.g. If [[1, 3]] -> 合并1,3列作為一個日期列使用
- dict, e.g. {‘foo’ : [1, 3]} -> 將1,3列合并,并給合并后的列起名為"foo"
to_csv
DataFrame.to_csv(path_or_buf=None,?sep=',?',?na_rep='',?float_format=None,?columns=None,?header=True,?index=True,?index_label=None,?mode='w',?encoding=None,?compression=None,?quoting=None,?quotechar='"',?line_terminator='\n',?chunksize=None,?tupleize_cols=False,?date_format=None,?doublequote=True,?escapechar=None,?decimal='.',?**kwds)Write DataFrame to a comma-separated values (csv) file
| path_or_buf?: string or file handle, default None File path or object, if None is provided the result is returned as a string. sep?: character, default ‘,’ Field delimiter for the output file. na_rep?: string, default ‘’ Missing data representation float_format?: string, default None Format string for floating point numbers columns?: sequence, optional Columns to write header?: boolean or list of string, default True Write out column names. If a list of string is given it is assumed to be aliases for the column names index?: boolean, default True Write row names (index) index_label?: string or sequence, or False, default None Column label for index column(s) if desired. If None is given, and?header?and?index?are True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex. If False do not print fields for index names. Use index_label=False for easier importing in R nanRep?: None deprecated, use na_rep mode?: str Python?write mode, default ‘w’ encoding?: string, optional A string representing the encoding to use in the output file, defaults to ‘ascii’ on Python 2 and ‘utf-8’ on Python 3. compression?: string, optional a string representing the compression to use in the output file, allowed values are ‘gzip’, ‘bz2’, ‘xz’, only used when the first argument is a filename line_terminator?: string, default ‘n’ The newline character or character sequence to use in the output file quoting?: optional constant from csv module defaults to csv.QUOTE_MINIMAL quotechar?: string (length 1), default ‘”’ character used to quote fields doublequote?: boolean, default True Control quoting of?quotechar?inside a field escapechar?: string (length 1), default None character used to escape?sep?and?quotechar?when appropriate chunksize?: int or None rows to write at a time tupleize_cols?: boolean, default False write multi_index columns as a list of tuples (if True) or new (expanded format) if False) date_format?: string, default None Format string for datetime objects decimal: string, default ‘.’ Character recognized as decimal separator. E.g. use ‘,’ for European data New in version 0.16.0. |
總結
以上是生活随笔為你收集整理的pandas中to_csv()和read_csv()参数详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用pip安装GDAL时出错
- 下一篇: python pandas.DataFr