天天看點

Py之pandas:pandas的read_excel()函數中各參數說明及函數使用方法講解(二)

io

str, bytes, ExcelFile, xlrd.Book, path object, or file-like object

Any valid string path is acceptable. The string could be a URL. Valid URL schemes include http, ftp, s3, and file. For file URLs, a host is expected. A local file could be: file://localhost/path/to/table.xlsx.  If you want to pass in a path object, pandas accepts any os.PathLike.  By file-like object, we refer to objects with a read() method, such as a file handler (e.g. via builtin open function) or StringIO.

str, bytes, ExcelFile, xlrd.Book, path object, , or file-like object

任何有效的字元串路徑。字元串可以是URL。有效的URL方案包括http、ftp、s3和file。對于檔案url,需要一個主機。本地檔案可以是:file://localhost/path/to/table.xlsx。如果您想傳入一個path對象,pandas會接受任何類似os. path的東西。通過類檔案對象,我們使用read()方法引用對象,比如檔案處理程式(例如通過内置的open函數)或StringIO。

sheet_name

str, int, list, or None, default 0

Strings are used for sheet names. Integers are used in zero-indexed sheet positions. Lists of strings/integers are used to request multiple sheets. Specify None to get all sheets.

Available cases:

Defaults to 0: 1st sheet as a DataFrame

1: 2nd sheet as a DataFrame

"Sheet1": Load sheet with name “Sheet1”

[0, 1, "Sheet5"]: Load first, second and sheet named “Sheet5” as a dict of DataFrame

None: All sheets.

表格名稱使用str、int、list或None等預設0字元串。整數用于零索引的工作表位置。字元串/整數清單用于請求多個表。指定None以擷取所有表。可用情況:預設為0:第1張表作為DataFrame1:第2張表作為DataFrame“Sheet1”:加載名稱為“Sheet1”的表[0,1,“Sheet5”]:首先加載,第二張和名為“Sheet5”的表作為DataFrameNone的dict:所有表。

sheet_name=sheetname_ID, 指定要讀入的sheet名

header

int, list of int, default 0

Row (0-indexed) to use for the column labels of the parsed DataFrame. If a list of integers is passed those row positions will be combined into a MultiIndex. Use None if there is no header.

行(0索引),用于已解析的資料格式的列标簽。如果傳遞了一個整數清單,那麼這些行位置将合并到一個多索引中。如果沒有标題,則使用None。

names

array-like, default None

List of column names to use. If file contains no header row, then you should explicitly pass header=None.

要使用的列名的清單。如果檔案不包含頭行,那麼應該顯式傳遞header=None。

使用names參數時,類似重命名。切記,要與原資料全部比對:即不能多也不能少,多或者少都會抛出ValueError錯誤。

index_col

int, list of int, default None

Column (0-indexed) to use as the row labels of the DataFrame. Pass None if there is no such column. If a list is passed, those columns will be combined into a MultiIndex. If a subset of data is selected with usecols, index_col is based on the subset.

Column(0索引)用作資料格式的行标簽。如果沒有這樣的列,則傳遞None。如果傳遞了一個清單,那麼這些列将被合并到一個多索引中。如果使用usecols選擇了資料子集,則index_col基于該子集。

index_col=0,      #不讀取第一索引列

usecols

int, str, list-like, or callable default None

If None, then parse all columns.

If str, then indicates comma separated list of Excel column letters and column ranges (e.g. “A:E” or “A,C,E:F”). Ranges are inclusive of both sides.

If list of int, then indicates list of column numbers to be parsed.

If list of string, then indicates list of column names to be parsed.

New in version 0.24.0.

If callable, then evaluate each column name against it and parse the column if the callable returns True.

Returns a subset of the columns according to behavior above.

如果沒有,那麼解析所有列。

如果str,則表示用逗号分隔的Excel列字母和列範圍清單(如“A:E”或“A,C,E:F”)。範圍包括兩邊。

如果list of int,則訓示要解析的列号清單。

如果字元串清單,則訓示要解析的列名清單。

新版本為0.24.0。

usecols=[1,2,7,8,14]

usecols參數,指定要讀入列的索引ID

squeeze

bool, default False

If the parsed data only contains one column then return a Series.

如果解析的資料隻包含一列,則傳回一個序列。

dtype

Type name or dict of column -> type, default None

Data type for data or columns. E.g. {‘a’: np.float64, ‘b’: np.int32} Use object to preserve data as stored in Excel and not interpret dtype. If converters are specified, they will be applied INSTEAD of dtype conversion.

資料或列的資料類型。例如{a: np。使用object儲存存儲在Excel中的資料,而不是解釋dtype。如果指定了轉換器,則将應用它們而不是dtype轉換。

engine

str, default None

If io is not a buffer or path, this must be set to identify io. Supported engines: “xlrd”, “openpyxl”, “odf”, “pyxlsb”, default “xlrd”. Engine compatibility : - “xlrd” supports most old/new Excel file formats. - “openpyxl” supports newer Excel file formats. - “odf” supports OpenDocument file formats (.odf, .ods, .odt). - “pyxlsb” supports Binary Excel files.

如果io不是緩沖區或路徑,則必須将其設定為識别io。支援的引擎:“xlrd”、“openpyxl”、“odf”、“pyxlsb”,預設為“xlrd”。引擎相容性:-“xlrd”支援大多數新舊Excel檔案格式。- " openpyxl "支援較新的Excel檔案格式。-“odf”支援OpenDocument檔案格式(。odf。ods, .odt)。- " pyxlsb "支援二進制Excel檔案。

converters

dict, default None

Dict of functions for converting values in certain columns. Keys can either be integers or column labels, values are functions that take one input argument, the Excel cell content, and return the transformed content.

用于轉換某些列中的值的函數的字典。鍵可以是整數也可以是列标簽,值是接受一個輸入參數Excel單元格内容并傳回轉換後内容的函數。

true_values

list, default None

Values to consider as True.

true_values

false_values

Values to consider as False.

false_values

skiprows

list-like

Rows to skip at the beginning (0-indexed).

開頭要跳過的行(0索引)。

nrows

int, default None

Number of rows to parse.

New in version 0.23.0.

要解析的行數。

新版本0.23.0。

na_values

scalar, str, list-like, or dict, default None

Additional strings to recognize as NA/NaN. If dict passed, specific per-column NA values. By default the following values are interpreted as NaN: ‘’, ‘#N/A’, ‘#N/A N/A’, ‘#NA’, ‘-1.#IND’, ‘-1.#QNAN’, ‘-NaN’, ‘-nan’, ‘1.#IND’, ‘1.#QNAN’, ‘<NA>’, ‘N/A’, ‘NA’, ‘NULL’, ‘NaN’, ‘n/a’, ‘nan’, ‘null’.

附加的弦可以像NA/NaN那樣識别。如果命中注定,具體的評估。例如:“‘N/A’、‘N/A’、‘N/A’、‘NA’、‘-1’。”# IND”、“錄音。QNAN ', ' -NaN ', ' -NaN ', ' 1。# IND”、“1。# QNAN NA系”、“< >”、“N / A”、“NA”、“空”、“南”、“N / A‘南’,‘空’。

keep_default_na

bool, default True

Whether or not to include the default NaN values when parsing the data. Depending on whether na_values is passed in, the behavior is as follows:

If keep_default_na is True, and na_values are specified, na_values is appended to the default NaN values used for parsing.

If keep_default_na is True, and na_values are not specified, only the default NaN values are used for parsing.

If keep_default_na is False, and na_values are specified, only the NaN values specified na_values are used for parsing.

If keep_default_na is False, and na_values are not specified, no strings will be parsed as NaN.

Note that if na_filter is passed in as False, the keep_default_na and na_values parameters will be ignored.

解析資料時是否包含預設的NaN值。根據是否傳入na_values,行為如下:

如果keep_default_na為真,并且指定了na_values,那麼na_values将附加到用于解析的預設NaN值中。

如果keep_default_na為真,并且沒有指定na_values,則隻使用預設的NaN值進行解析。

如果keep_default_na為False,并且指定了na_values,則僅使用指定na_values的NaN值進行解析。

如果keep_default_na為False,并且沒有指定na_values,則不會将任何字元串解析為NaN。

注意,如果将na_filter作為False傳入,則keep_default_na和na_values參數将被忽略。

na_filter

Detect missing value markers (empty strings and the value of na_values). In data without any NAs, passing na_filter=False can improve the performance of reading a large file.

檢測缺失的值标記(空字元串和na_values的值)。在沒有NAs的資料中,傳遞na_filter=False可以提高讀取大檔案的性能。

verbose

Indicate number of NA values placed in non-numeric columns.

訓示放置在非數字列中的NA值的數目。

parse_dates

bool, list-like, or dict, default False

The behavior is as follows:

bool. If True -> try parsing the index.

list of int or names. e.g. If [1, 2, 3] -> try parsing columns 1, 2, 3 each as a separate date column.

list of lists. e.g. If [[1, 3]] -> combine columns 1 and 3 and parse as a single date column.

dict, e.g. {‘foo’ : [1, 3]} -> parse columns 1, 3 as date and call result ‘foo’

If a column or index contains an unparseable date, the entire column or index will be returned unaltered as an object data type. If you don`t want to parse some cells as date just change their type in Excel to “Text”. For non-standard datetime parsing, use pd.to_datetime after pd.read_excel.

Note: A fast-path exists for iso8601-formatted dates.

其行為如下:

bool類型:如果為真——>嘗試解析索引。

int或名稱的清單。例如,If[1,2,3] ->嘗試将1,2,3列分别解析為一個單獨的日期列。

list類型:例如,If[[1,3]] ->組合列1和3并解析為單個日期列。

dict類型:例如{' foo ':[1,3]} ->解析列1,3作為日期并調用結果' foo '

如果列或索引包含不可解析的日期,則整個列或索引将作為對象資料類型不變地傳回。如果你不想把一些單元格解析為date,那就把它們在Excel中的類型改為Text。對于非标準的日期時間解析,在pd.read_excel後面使用pd.to_datetime。

注意:有一個用于iso8601格式的日期的快速路徑。

date_parser

function, optional

Function to use for converting a sequence of string columns to an array of datetime instances. The default uses dateutil.parser.parser to do the conversion. Pandas will try to call date_parser in three different ways, advancing to the next if an exception occurs: 1) Pass one or more arrays (as defined by parse_dates) as arguments; 2) concatenate (row-wise) the string values from the columns defined by parse_dates into a single array and pass that; and 3) call date_parser once for each row using one or more strings (corresponding to the columns defined by parse_dates) as arguments.

該函數,用于将字元串列序列轉換為日期時間執行個體數組。預設使用dateutil.parser。解析器執行轉換。熊貓将嘗試以三種不同的方式調用date_parser,如果出現異常,則繼續調用:1)傳遞一個或多個數組(由parse_date定義)作為參數;2)将parse_date定義的列中的字元串值連接配接到一個數組中并傳遞它;使用一個或多個字元串(對應于parse_date定義的列)作為參數,對每一行調用date_parser一次。

thousands

Thousands separator for parsing string columns to numeric. Note that this parameter is only necessary for columns stored as TEXT in Excel, any numeric columns will automatically be parsed, regardless of display format.

數以千計的分隔符用于将字元串列解析為數字。請注意,此參數僅對存儲為文本的列在Excel中是必要的,任何數值列都将自動解析,無論顯示格式如何。

comment

Comments out remainder of line. Pass a character or characters to this argument to indicate comments in the input file. Any data between the comment string and the end of the current line is ignored.

注釋掉行中的餘數。向此參數傳遞一個或多個字元,以訓示輸入檔案中的注釋。注釋字元串和目前行結束之間的任何資料都将被忽略。

skipfooter

int, default 0

Rows at the end to skip (0-indexed).

末尾要跳過的行(0索引)。

convert_float

Convert integral floats to int (i.e., 1.0 –> 1). If False, all numeric data will be read in as floats: Excel stores all numbers as floats internally.

将整型浮點數轉換為整型浮點數(例如,1.0 - > - 1),如果為False,則所有數值資料将以浮點數的形式讀入:Excel在内部将所有數字存儲為浮點數。

mangle_dupe_cols

Duplicate columns will be specified as ‘X’, ‘X.1’, …’X.N’, rather than ‘X’…’X’. Passing in False will cause data to be overwritten if there are duplicate names in the columns.

重複列将被指定為' X ', ' X。1 ',…”X。是N,而不是X,是X。如果列中有重複的名稱,傳入False将導緻資料被覆寫。

Returns

DataFrame or dict of DataFrames

DataFrame from the passed in Excel file. See notes in sheet_name argument for more information on when a dict of DataFrames is returned.

DataFrame從傳遞的Excel檔案。請參閱sheet_name參數中的注釋,以獲得關于何時傳回資料變量的更多資訊。