日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pandas中to_csv()和read_csv()参数详解

發(fā)布時間:2024/10/6 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pandas中to_csv()和read_csv()参数详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

pandas.read_csv參數(shù)整理

讀取CSV(逗號分割)文件到DataFrame 也支持文件的部分導(dǎo)入和選擇迭代 更多幫助參見:http://pandas.pydata.org/pandas-docs/stable/io.html 參數(shù): 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和文件。對于多文件正在準(zhǔn)備中 本地文件讀取實例:://localhost/path/to/table.csv sep?: str, default ‘,’ 指定分隔符。如果不指定參數(shù),則會嘗試使用逗號分隔。分隔符長于一個字符并且不是‘\s+’,將使用python的語法分析器。并且忽略數(shù)據(jù)中的逗號。正則表達(dá)式例子:'\r\t' delimiter?: str, default?None 定界符,備選分隔符(如果指定該參數(shù),則sep參數(shù)失效) delim_whitespace?: boolean, default False. 指定空格(例如’ ‘或者’ ‘)是否作為分隔符使用,等效于設(shè)定sep='\s+'。如果這個參數(shù)設(shè)定為Ture那么delimiter 參數(shù)失效。 在新版本0.18.1支持 header?: int or list of ints, default ‘infer’ 指定行數(shù)用來作為列名,數(shù)據(jù)開始行數(shù)。如果文件中沒有列名,則默認(rèn)為0,否則設(shè)置為None。如果明確設(shè)定header=0 就會替換掉原來存在列名。header參數(shù)可以是一個list例如:[0,1,3],這個list表示將文件中的這些行作為列標(biāo)題(意味著每一列有多個標(biāo)題),介于中間的行將被忽略掉(例如本例中的2;本例中的數(shù)據(jù)1,2,4行將被作為多級標(biāo)題出現(xiàn),第3行數(shù)據(jù)將被丟棄,dataframe的數(shù)據(jù)從第5行開始。)。 注意:如果skip_blank_lines=True 那么header參數(shù)忽略注釋行和空行,所以header=0表示第一行數(shù)據(jù)而不是文件的第一行。 names?: array-like, default None 用于結(jié)果的列名列表,如果數(shù)據(jù)文件中沒有列標(biāo)題行,就需要執(zhí)行header=None。默認(rèn)列表中不能出現(xiàn)重復(fù),除非設(shè)定參數(shù)mangle_dupe_cols=True。 index_col?: int or sequence or False, default None 用作行索引的列編號或者列名,如果給定一個序列則有多個行索引。 如果文件不規(guī)則,行尾有分隔符,則可以設(shè)定index_col=False 來是的pandas不適用第一列作為行索引。 usecols?: array-like, default None 返回一個數(shù)據(jù)子集,該列表中的值必須可以對應(yīng)到文件中的位置(數(shù)字可以對應(yīng)到指定的列)或者是字符傳為文件中的列名。例如:usecols有效參數(shù)可能是 [0,1,2]或者是 [‘foo’, ‘bar’, ‘baz’]。使用這個參數(shù)可以加快加載速度并降低內(nèi)存消耗。 as_recarray?: boolean, default False 不贊成使用:該參數(shù)會在未來版本移除。請使用pd.read_csv(...).to_records()替代。 返回一個Numpy的recarray來替代DataFrame。如果該參數(shù)設(shè)定為True。將會優(yōu)先squeeze參數(shù)使用。并且行索引將不再可用,索引列也將被忽略。 squeeze?: boolean, default False 如果文件值包含一列,則返回一個Series prefix?: str, default None 在沒有列標(biāo)題時,給列添加前綴。例如:添加‘X’ 成為 X0, X1, ... mangle_dupe_cols?: boolean, default True 重復(fù)的列,將‘X’...’X’表示為‘X.0’...’X.N’。如果設(shè)定為false則會將所有重名列覆蓋。 dtype?: Type name or dict of column -> type, default None 每列數(shù)據(jù)的數(shù)據(jù)類型。例如 {‘a(chǎn)’: 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 列轉(zhuǎn)換函數(shù)的字典。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 忽略分隔符后的空白(默認(rèn)為False,即不忽略). skiprows?: list-like or integer, default None 需要忽略的行數(shù)(從文件開始處算起),或需要跳過的行號列表(從0開始)。 skipfooter?: int, default 0 從文件尾部開始忽略。 (c引擎不支持) skip_footer?: int, default 0 不推薦使用:建議使用skipfooter?,功能一樣。 nrows?: int, default None 需要讀取的行數(shù)(從文件頭開始算起)。 na_values?: scalar, str, list-like, or dict, default None 一組用于替換NA/NaN的值。如果傳參,需要制定特定列的空值。默認(rèn)為‘1.#IND’, ‘1.#QNAN’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘nan’`. keep_default_na?: bool, default True 如果指定na_values參數(shù),并且keep_default_na=False,那么默認(rèn)的NaN將被覆蓋,否則添加。 na_filter?: boolean, default True 是否檢查丟失值(空字符串或者是空值)。對于大文件來說數(shù)據(jù)集中沒有空值,設(shè)定na_filter=False可以提升讀取速度。 verbose?: boolean, default False 是否打印各種解析器的輸出信息,例如:“非數(shù)值列中缺失值的數(shù)量”等。 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列的值作為獨(dú)立的日期列;
  • list of lists. e.g. If [[1, 3]] -> 合并1,3列作為一個日期列使用
  • dict, e.g. {‘foo’ : [1, 3]} -> 將1,3列合并,并給合并后的列起名為"foo"
infer_datetime_format?: boolean, default False 如果設(shè)定為True并且parse_dates?可用,那么pandas將嘗試轉(zhuǎn)換為日期類型,如果可以轉(zhuǎn)換,轉(zhuǎn)換方法并解析。在某些情況下會快5~10倍。 keep_date_col?: boolean, default False 如果連接多列解析日期,則保持參與連接的列。默認(rèn)為False。 date_parser?: function, default None 用于解析日期的函數(shù),默認(rèn)使用dateutil.parser.parser來做轉(zhuǎn)換。Pandas嘗試使用三種不同的方式解析,如果遇到問題則使用下一種方式。 1.使用一個或者多個arrays(由parse_dates指定)作為參數(shù); 2.連接指定多列字符串作為一個列作為參數(shù); 3.每行調(diào)用一次date_parser函數(shù)來解析一個或者多個字符串(由parse_dates指定)作為參數(shù)。 dayfirst?: boolean, default False DD/MM格式的日期類型 iterator?: boolean, default False 返回一個TextFileReader 對象,以便逐塊處理文件。 chunksize?: int, default None 文件塊的大小,?See IO Tools docs for more informationon?iterator?and?chunksize. compression?: {‘infer’, ‘gzip’, ‘bz2’, ‘zip’, ‘xz’, None}, default ‘infer’ 直接使用磁盤上的壓縮文件。如果使用infer參數(shù),則使用 gzip, bz2, zip或者解壓文件名中以‘.gz’, ‘.bz2’, ‘.zip’, or ‘xz’這些為后綴的文件,否則不解壓。如果使用zip,那么ZIP包中國必須只包含一個文件。設(shè)置為None則不解壓。 新版本0.18.1版本支持zip和xz解壓 thousands?: str, default None 千分位分割符,如“,”或者“." decimal?: str, default ‘.’ 字符中的小數(shù)點 (例如:歐洲數(shù)據(jù)使用’,‘). float_precision?: string, default None Specifies which converter the C engine should use for floating-point values. The options are?None?for the ordinary converter,?high?for the high-precision converter, and?round_trip?for the round-trip converter. 指定 lineterminator?: str (length 1), default None 行分割符,只在C解析器下使用。 quotechar?: str (length 1), optional 引號,用作標(biāo)識開始和解釋的字符,引號內(nèi)的分割符將被忽略。 quoting?: int or csv.QUOTE_* instance, default 0 控制csv中的引號常量。可選 QUOTE_MINIMAL (0), QUOTE_ALL (1), QUOTE_NONNUMERIC (2) or QUOTE_NONE (3) doublequote?: boolean, default?True 雙引號,當(dāng)單引號已經(jīng)被定義,并且quoting 參數(shù)不是QUOTE_NONE的時候,使用雙引號表示引號內(nèi)的元素作為一個元素使用。 escapechar?: str (length 1), default None 當(dāng)quoting 為QUOTE_NONE時,指定一個字符使的不受分隔符限值。 comment?: str, default None 標(biāo)識著多余的行不被解析。如果該字符出現(xiàn)在行首,這一行將被全部忽略。這個參數(shù)只能是一個字符,空行(就像skip_blank_lines=True)注釋行被header和skiprows忽略一樣。例如如果指定comment='#' 解析‘#empty\na,b,c\n1,2,3’ 以header=0 那么返回結(jié)果將是以’a,b,c'作為header。 encoding?: str, default None 指定字符集類型,通常指定為'utf-8'.?List of Python standard encodings dialect?: str or csv.Dialect instance, default None 如果沒有指定特定的語言,如果sep大于一個字符則忽略。具體查看csv.Dialect 文檔 tupleize_cols?: boolean, default False Leave a list of tuples on columns as is (default is to convert to a Multi Index on the columns) error_bad_lines?: boolean, default True 如果一行包含太多的列,那么默認(rèn)不會返回DataFrame ,如果設(shè)置成false,那么會將改行剔除(只能在C解析器下使用)。 warn_bad_lines?: boolean, default True 如果error_bad_lines =False,并且warn_bad_lines =True 那么所有的“bad lines”將會被輸出(只能在C解析器下使用)。 low_memory?: boolean, default True 分塊加載到內(nèi)存,再低內(nèi)存消耗中解析。但是可能出現(xiàn)類型混淆。確保類型不被混淆需要設(shè)置為False。或者使用dtype?參數(shù)指定類型。注意使用chunksize?或者iterator?參數(shù)分塊讀入會將整個文件讀入到一個Dataframe,而忽略類型(只能在C解析器中有效) buffer_lines?: int, default None 不推薦使用,這個參數(shù)將會在未來版本移除,因為他的值在解析器中不推薦使用 compact_ints?: boolean, default False 不推薦使用,這個參數(shù)將會在未來版本移除 如果設(shè)置compact_ints=True ,那么任何有整數(shù)類型構(gòu)成的列將被按照最小的整數(shù)類型存儲,是否有符號將取決于use_unsigned?參數(shù) use_unsigned?: boolean, default False 不推薦使用:這個參數(shù)將會在未來版本移除 如果整數(shù)列被壓縮(i.e.?compact_ints=True),指定被壓縮的列是有符號還是無符號的。 memory_map?: boolean, default False 如果使用的文件在內(nèi)存內(nèi),那么直接map文件使用。使用這種方式可以避免文件再次進(jìn)行IO操作。

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

Parameters:

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 ‘a(chǎn)scii’ 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.


總結(jié)

以上是生活随笔為你收集整理的pandas中to_csv()和read_csv()参数详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。