如何 套用模板做網(wǎng)站線上營銷模式
目錄
一、用法精講
7、pandas.read_clipboard函數(shù)
7-1、語法
7-2、參數(shù)
7-3、功能
7-4、返回值
7-5、說明
7-6、用法
7-6-1、代碼示例
7-6-2、結(jié)果輸出
8、pandas.DataFrame.to_clipboard函數(shù)
8-1、語法
8-2、參數(shù)
8-3、功能
8-4、返回值
8-5、說明
8-6、用法
8-6-1、代碼示例
8-6-2、結(jié)果輸出?
9、pandas.read_excel函數(shù)
9-1、語法
9-2、參數(shù)
9-3、功能
9-4、返回值
9-5、說明
9-6、用法
9-6-1、數(shù)據(jù)準備
9-6-2、代碼示例
9-6-3、結(jié)果輸出
二、推薦閱讀
1、Python筑基之旅
2、Python函數(shù)之旅
3、Python算法之旅
4、Python魔法之旅
5、博客個人主頁
一、用法精講
7、pandas.read_clipboard函數(shù)
7-1、語法
# 7、pandas.read_clipboard函數(shù)
pandas.read_clipboard(sep='\\s+', dtype_backend=_NoDefault.no_default, **kwargs)
Read text from clipboard and pass to read_csv().Parses clipboard contents similar to how CSV files are parsed using read_csv().Parameters:
sepstr, default ‘\s+’
A string or regex delimiter. The default of '\\s+' denotes one or more whitespace characters.dtype_backend{‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’
Back-end data type applied to the resultant DataFrame (still experimental). Behaviour is as follows:"numpy_nullable": returns nullable-dtype-backed DataFrame (default)."pyarrow": returns pyarrow-backed nullable ArrowDtype DataFrame.New in version 2.0.**kwargs
See read_csv() for the full argument list.Returns:
DataFrame
A parsed DataFrame object.
7-2、參數(shù)
7-2-1、sep(可選,默認值為'\\s+'):表示使用正則表達式來匹配一個或多個空白字符(如空格、制表符等)作為字段之間的分隔符,這意味著,如果你的數(shù)據(jù)是通過空格、制表符等分隔的,你可以直接使用默認值,但如果你使用的是逗號(CSV 格式)或其他分隔符,你應(yīng)該相應(yīng)地更改這個參數(shù),比如sep=','。
7-2-2、dtype_backend(可選):這個參數(shù)通常不需要用戶直接設(shè)置,它是用來指定數(shù)據(jù)類型推斷的后端,Pandas內(nèi)部使用它來優(yōu)化數(shù)據(jù)類型的推斷過程。
7-2-3、**kwargs(可選):一個可變關(guān)鍵字參數(shù),允許你傳遞額外的參數(shù)給函數(shù),這些參數(shù)會被傳遞給pandas.read_csv()函數(shù),因為read_clipboard()在內(nèi)部實際上是使用read_csv()來解析剪貼板中的數(shù)據(jù)。因此,你可以傳遞任何read_csv()支持的參數(shù),比如header(指定列名的行索引,默認為0,如果沒有列名則為None)、index_col(用作行索引的列編號或列名列表)、dtype(指定列的數(shù)據(jù)類型)等。
7-3、功能
????????從用戶的系統(tǒng)剪貼板中讀取文本數(shù)據(jù),并將其解析為pandas DataFrame對象。
7-4、返回值
????????返回值是一個pandas DataFrame對象,該對象包含了剪貼板中解析后的數(shù)據(jù),其中每行代表數(shù)據(jù)表中的一行,每列代表數(shù)據(jù)表中的一個字段。DataFrame的索引、列名和數(shù)據(jù)類型等屬性會根據(jù)剪貼板中的數(shù)據(jù)和函數(shù)的參數(shù)設(shè)置自動推斷和設(shè)置。
7-5、說明
? ? ? ? 從Pandas 1.0.0開始,dtype_backend參數(shù)已被棄用,并且可能在未來的版本中移除。在大多數(shù)情況下,用戶不需要直接設(shè)置這個參數(shù)。
7-6、用法
7-6-1、代碼示例
# 7、pandas.read_clipboard函數(shù)
# 7-1、先復(fù)制以下內(nèi)容
# Name Age City
# Alice 30 New York
# Bob 25 Los Angeles
# Charlie 35 Chicago# 7-2、使用pandas.read_clipboard()函數(shù)讀取剪切板的信息
import pandas as pd
# 讀取剪貼板中的數(shù)據(jù),指定分隔符為制表符
df = pd.read_clipboard(sep='\t')
# 顯示 DataFrame
print(df)
7-6-2、結(jié)果輸出
# Name Age City
# 0 Alice 30 New York
# 1 Bob 25 Los Angeles
# 2 Charlie 35 Chicago
8、pandas.DataFrame.to_clipboard函數(shù)
8-1、語法
# 8、pandas.DataFrame.to_clipboard函數(shù)
DataFrame.to_clipboard(*, excel=True, sep=None, **kwargs)
Copy object to the system clipboard.Write a text representation of object to the system clipboard. This can be pasted into Excel, for example.Parameters:
excelbool, default True
Produce output in a csv format for easy pasting into excel.True, use the provided separator for csv pasting.False, write a string representation of the object to the clipboard.sepstr, default '\t'
Field delimiter.**kwargs
These parameters will be passed to DataFrame.to_csv.
8-2、參數(shù)
8-2-1、excel(可選,默認值為True):如果為True,則嘗試以Excel友好的方式復(fù)制數(shù)據(jù),即如果可能的話,會保留多個工作表或樣式。但是,請注意,由于剪貼板本身并不支持復(fù)雜的數(shù)據(jù)結(jié)構(gòu)(如多個工作表或樣式),因此這個參數(shù)的實際效果可能因操作系統(tǒng)和剪貼板支持的功能而異。在大多數(shù)情況下,將其設(shè)置為True或False對結(jié)果沒有顯著影響,因為剪貼板通常只接受純文本或CSV格式的數(shù)據(jù)。
8-2-2、sep(可選,默認值為None):用于分隔DataFrame中列的分隔符。如果為None(默認值),則不會添加任何分隔符,DataFrame會以制表符分隔的格式(類似于CSV但沒有引號包圍字符串)復(fù)制到剪貼板。如果你想要使用逗號(,)或其他字符作為分隔符,可以指定該參數(shù)。但是,請注意,不是所有的應(yīng)用程序都能很好地處理從剪貼板粘貼的自定義分隔符數(shù)據(jù)。
8-2-3、**kwargs(可選):一個可變關(guān)鍵字參數(shù),允許你傳遞額外的參數(shù)給底層的to_csv()方法(盡管在大多數(shù)情況下,to_clipboard()方法并不直接調(diào)用to_csv(),但它們的參數(shù)在某些方面相似)。然而,對于to_clipboard()方法來說,**kwargs實際上并不接受與to_csv()相同的所有參數(shù),因為剪貼板操作有其自身的限制和特性。
8-3、功能
????????將pandas DataFrame對象的內(nèi)容復(fù)制到系統(tǒng)的剪貼板中。
8-4、返回值
????????不返回任何值(即返回值為None),它的主要作用是將DataFrame的內(nèi)容復(fù)制到剪貼板,而不是返回一個新的對象或數(shù)據(jù)。
8-5、說明
????????用戶就可以方便地將DataFrame中的數(shù)據(jù)粘貼到其他應(yīng)用程序中,如Excel、Word或其他文本編輯器,以便進一步的處理或展示。
8-6、用法
8-6-1、代碼示例
# 8、pandas.DataFrame.to_clipboard函數(shù)
# 8-1、將pandas DataFrame對象的內(nèi)容復(fù)制到系統(tǒng)的剪貼板中
import pandas as pd
# 創(chuàng)建一個示例 DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],'Age': [24, 30, 22],'City': ['New York', 'San Francisco', 'Los Angeles']}
df = pd.DataFrame(data)
# 將 DataFrame 的內(nèi)容復(fù)制到剪貼板
df.to_clipboard(index=False) # 注意:雖然這里寫了 index=False,但 to_clipboard 并不接受這個參數(shù)
# 此時,你可以在其他應(yīng)用程序中粘貼 DataFrame 的內(nèi)容# 8-2、在打開的excel、word及編輯器中粘貼操作(注:Ctrl+V)
8-6-2、結(jié)果輸出?
# Name Age City
# Alice 24 New York
# Bob 30 San Francisco
# Charlie 22 Los Angeles
9、pandas.read_excel函數(shù)
9-1、語法
# 9、pandas.read_excel函數(shù)
pandas.read_excel(io, sheet_name=0, *, header=0, names=None, index_col=None, usecols=None, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, na_filter=True, verbose=False, parse_dates=False, date_parser=_NoDefault.no_default, date_format=None, thousands=None, decimal='.', comment=None, skipfooter=0, storage_options=None, dtype_backend=_NoDefault.no_default, engine_kwargs=None)
Read an Excel file into a pandas DataFrame.Supports xls, xlsx, xlsm, xlsb, odf, ods and odt file extensions read from a local filesystem or URL. Supports an option to read a single sheet or a list of sheets.Parameters:
iostr, 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 handle (e.g. via builtin open function) or StringIO.Deprecated since version 2.1.0: Passing byte strings is deprecated. To read from a byte string, wrap it in a BytesIO object.sheet_namestr, int, list, or None, default 0
Strings are used for sheet names. Integers are used in zero-indexed sheet positions (chart sheets do not count as a sheet position). Lists of strings/integers are used to request multiple sheets. Specify None to get all worksheets.Available cases:Defaults to 0: 1st sheet as a DataFrame1: 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 DataFrameNone: All worksheets.headerint, 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.namesarray-like, default None
List of column names to use. If file contains no header row, then you should explicitly pass header=None.index_colint, str, 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.Missing values will be forward filled to allow roundtripping with to_excel for merged_cells=True. To avoid forward filling the missing values use set_index after reading the data instead of index_col.usecolsstr, 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 (0-indexed).If list of string, then indicates list of column names to be parsed.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.dtypeType name or dict of column -> type, default None
Data type for data or columns. E.g. {‘a(chǎn)’: np.float64, ‘b’: np.int32} Use object to preserve data as stored in Excel and not interpret dtype, which will necessarily result in object dtype. If converters are specified, they will be applied INSTEAD of dtype conversion. If you use None, it will infer the dtype of each column based on the data.engine{‘openpyxl’, ‘calamine’, ‘odf’, ‘pyxlsb’, ‘xlrd’}, default None
If io is not a buffer or path, this must be set to identify io. Engine compatibility :openpyxl supports newer Excel file formats.calamine supports Excel (.xls, .xlsx, .xlsm, .xlsb) and OpenDocument (.ods) file formats.odf supports OpenDocument file formats (.odf, .ods, .odt).pyxlsb supports Binary Excel files.xlrd supports old-style Excel files (.xls).When engine=None, the following logic will be used to determine the engine:If path_or_buffer is an OpenDocument format (.odf, .ods, .odt), then odf will be used.Otherwise if path_or_buffer is an xls format, xlrd will be used.Otherwise if path_or_buffer is in xlsb format, pyxlsb will be used.Otherwise openpyxl will be used.convertersdict, 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.true_valueslist, default None
Values to consider as True.false_valueslist, default None
Values to consider as False.skiprowslist-like, int, or callable, optional
Line numbers to skip (0-indexed) or number of lines to skip (int) at the start of the file. If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False otherwise. An example of a valid callable argument would be lambda x: x in [0, 2].nrowsint, default None
Number of rows to parse.na_valuesscalar, 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’, ‘None’, ‘n/a’, ‘nan’, ‘null’.keep_default_nabool, 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.na_filterbool, default True
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.verbosebool, default False
Indicate number of NA values placed in non-numeric columns.parse_datesbool, 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 unparsable 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.date_parserfunction, 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.Deprecated since version 2.0.0: Use date_format instead, or read in as object and then apply to_datetime() as-needed.date_formatstr or dict of column -> format, default None
If used in conjunction with parse_dates, will parse dates according to this format. For anything more complex, please read in as object and then apply to_datetime() as-needed.New in version 2.0.0.thousandsstr, default None
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.decimalstr, default ‘.’
Character to recognize as decimal point 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.(e.g. use ‘,’ for European data).New in version 1.4.0.commentstr, default None
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.skipfooterint, default 0
Rows at the end to skip (0-indexed).storage_optionsdict, optional
Extra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to urllib.request.Request as header options. For other URLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs are forwarded to fsspec.open. Please see fsspec and urllib for more details, and for more examples on storage options refer here.dtype_backend{‘numpy_nullable’, ‘pyarrow’}, default ‘numpy_nullable’
Back-end data type applied to the resultant DataFrame (still experimental). Behaviour is as follows:"numpy_nullable": returns nullable-dtype-backed DataFrame (default)."pyarrow": returns pyarrow-backed nullable ArrowDtype DataFrame.New in version 2.0.engine_kwargsdict, optional
Arbitrary keyword arguments passed to excel engine.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.
9-2、參數(shù)
9-2-1、io(必須):文件路徑、文件對象或ExcelFile對象,指定要讀取的Excel文件的路徑或?qū)ο蟆?/p>
9-2-2、sheet_name(可選,默認值為0):字符串、整數(shù)、字符串列表或None,指定要讀取的工作表(sheet)。如果是整數(shù),則按位置索引(從0開始);如果是字符串,則按名稱索引;如果是列表的字符串,則返回字典,其中鍵是工作表名,值是對應(yīng)的DataFrame;如果為None,則返回所有工作表作為字典。
9-2-3、header(可選,默認值為0):指定作為列名的行,默認為0(第一行)。如果文件沒有列名,則可以使用None 并通過names參數(shù)提供列名。
9-2-4、names(可選,默認值為None):列表,如果原始數(shù)據(jù)中不包含列標題,則可以通過此參數(shù)手動指定列標題。
9-2-5、index_col(可選,默認值為None):整數(shù)、字符串、序列或布爾值,用作行索引的列編號或列名。如果傳遞整數(shù),則按位置索引;如果傳遞字符串,則按名稱索引;如果是序列,則使用多個列作為多級索引;如果為False,則不使用任何列作為索引。
9-2-6、usecols(可選,默認值為None):整數(shù)、字符串、列表或可調(diào)用對象,如果為整數(shù),則只使用這一列;如果是字符串,則只使用此名稱的列;如果是列表,則使用這些索引或名稱的列;如果是可調(diào)用對象,則用于選擇列。
9-2-7、dtype(可選,默認值為None):類型名或字典,指定每列的數(shù)據(jù)類型。如果傳遞字典,則鍵是列名,值是類型名。
9-2-8、engine(可選,默認值為None):字符串,用于解析Excel文件的引擎。常用的有openpyxl(對于.xlsx文件)和xlrd(對于較舊的.xls文件)。注意,xlrd從版本2.0.0開始不再支持.xlsx文件。
9-2-9、converters(可選,默認值為None):字典,一個將列名映射到函數(shù)的字典,用于在讀取之前轉(zhuǎn)換列的值。
9-2-10、true_values/false_values(可選,默認值為None):列表樣對象,用于將值轉(zhuǎn)換為布爾值的序列。
9-2-11、skiprows(可選,默認值為None):列表樣對象,在讀取之前要跳過的行(從文件開始計數(shù))。
9-2-12、nrows(可選,默認值為None):整數(shù),要讀取的行數(shù)(從文件開始)。
9-2-13、na_values(可選,默認值為None):標量、字符串、列表樣對象或字典,用于將空值替換為NaN的額外值。
9-2-14、keep_default_na(可選,默認值為True):布爾值,如果為True,則使用pandas的默認NaN值集。
9-2-15、na_filter(可選,默認值為True):布爾值,如果為True,則嘗試檢測缺失值(如空字符串或僅包含空白的字符串)。
9-2-16、verbose(可選,默認值為False):布爾值,如果為True,則打印有關(guān)文件讀取的額外信息。
9-2-17、parse_dates(可選,默認值為False):布爾值、列表樣對象或字典,嘗試將數(shù)據(jù)解析為日期。如果為True,則嘗試解析所有列;如果為列表,則僅解析列表中的列;如果為字典,則字典的鍵是列名,值是要解析的日期格式。
9-2-18、date_parser(可選):用于解析日期的函數(shù)。
9-2-19、date_format(可選,默認值為None):日期時間對象的格式字符串。
9-2-20、thousands(可選,默認值為None):字符串,千位分隔符,如逗號(,)或點(.)。
9-2-21、decimal(可選,默認值為'.'):字符串,小數(shù)點字符。
9-2-22、comment(可選,默認值為None):字符串,表示注釋字符的字符串,用于跳過包含此字符的行。
9-2-23、skipfooter(可選,默認值為0):整數(shù),在文件末尾要跳過的行數(shù)(不支持所有引擎)。
9-2-24、storage_options(可選,默認值為None):字典,對于支持的文件類型(如AWS S3、Google Cloud Storage),可以傳遞額外的存儲選項。
9-2-25、dtype_backend(可選):這個參數(shù)通常不需要用戶直接設(shè)置,它是用來指定數(shù)據(jù)類型推斷的后端,Pandas內(nèi)部使用它來優(yōu)化數(shù)據(jù)類型的推斷過程。
9-2-27、engine_kwargs(可選,默認值為None):字典,傳遞給Excel讀取引擎的額外關(guān)鍵字參數(shù)。
9-3、功能
????????將Excel文件中的數(shù)據(jù)讀取到pandas的DataFrame對象中。
9-4、返回值
9-4-1、當只讀取一個工作表時,pandas.read_excel()函數(shù)返回一個pandas DataFrame對象,該對象包含了指定工作表中的所有數(shù)據(jù)。DataFrame是pandas中用于存儲和操作結(jié)構(gòu)化數(shù)據(jù)的主要數(shù)據(jù)結(jié)構(gòu),它類似于Excel中的表格,有行和列。
9-4-2、當讀取多個工作表時,如果sheet_name參數(shù)被設(shè)置為一個列表,包含了要讀取的工作表名稱或索引,則函數(shù)返回一個字典,鍵是工作表的名稱或索引,值是該工作表對應(yīng)的DataFrame對象,這樣,用戶就可以方便地訪問和操作多個工作表中的數(shù)據(jù)。
9-5、說明
????????通過這個函數(shù),用戶可以輕松地將存儲在Excel表格中的數(shù)據(jù)加載到pandas的數(shù)據(jù)結(jié)構(gòu)中,進而進行各種數(shù)據(jù)分析和處理操作。該函數(shù)支持從本地文件系統(tǒng)、URL或其他文件路徑讀取Excel文件,并提供了豐富的參數(shù)來自定義讀取過程,如指定工作表、列名、索引列、數(shù)據(jù)類型等。
9-6、用法
9-6-1、數(shù)據(jù)準備
9-6-2、代碼示例
# 9、pandas.read_excel函數(shù)
# 9-1、基本讀取
import pandas as pd
# 讀取 Excel 文件中的第一個工作表
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx')
print(df.head())# 9-2、讀取指定工作表
import pandas as pd
# 讀取名為 'Sheet2' 的工作表
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', sheet_name='Sheet2')
print(df.head())# 9-3、指定列名和索引列
import pandas as pd
# 指定第一行作為列名,第二列作為索引列
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', header=0, index_col=1)
print(df.head())# 9-4、讀取特定列
import pandas as pd
# 只讀取第1, 2, 3列
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', usecols=[0, 1, 2])
print(df.head())# 9-5、數(shù)據(jù)類型轉(zhuǎn)換
import pandas as pd
# 將第一列作為字符串讀取
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', dtype={0: str})
print(df.head())# 9-6、使用自定義缺失值
import pandas as pd
# 將 'NA' 和 'Missing' 視為缺失值
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', na_values=['NA', 'Missing'])
print(df.head())# 9-7、跳過行和讀取特定行數(shù)
import pandas as pd
# 跳過前兩行,讀取接下來的10行
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', skiprows=2, nrows=10)
print(df.head())# 9-8、日期解析
import pandas as pd
# 解析第一列為日期
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', parse_dates=[0])
print(df.head())# 9-9、讀取尾部行
import pandas as pd
# 跳過最后兩行
df = pd.read_excel('Pandas_read_excel數(shù)據(jù).xlsx', skipfooter=2)
print(df.head())
9-6-3、結(jié)果輸出
# 9-1、基本讀取
# 生產(chǎn)日期 班別 機臺 設(shè)備品牌 設(shè)備型號 ... 生產(chǎn)周期(s) 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 0 2024-07-04 A 1 YZM UN160SM2 ... 38.0 23.40 506 3236 12148
# 1 2024-07-04 A 3 YZM UN160SM2 ... 38.6 15.80 612 2448 120000
# 2 2024-07-04 A 5 YZM UN160A ... 30.1 2.85 2500 4800 2205
# 3 2024-07-04 A 7 NaN UN120A ... 28.6 2.40 3500 8500 31244
# 4 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 2800 417
#
# [5 rows x 16 columns]# 9-2、讀取指定工作表
# 生產(chǎn)日期 班別 機臺 設(shè)備品牌 設(shè)備型號 ... 生產(chǎn)周期(s) 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 0 2024-07-04 A 1 YZM UN160SM2 ... 38.0 23.40 506 3236 12148
# 1 2024-07-04 A 3 YZM UN160SM2 ... 38.6 15.80 612 2448 120000
# 2 2024-07-04 A 5 YZM UN160A ... 30.1 2.85 2500 4800 2205
# 3 2024-07-04 A 7 NaN UN120A ... 28.6 2.40 3500 8500 31244
# 4 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 2800 417
#
# [5 rows x 16 columns]# 9-3、指定列名和索引列
# 生產(chǎn)日期 機臺 設(shè)備品牌 設(shè)備型號 ... 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 班別 ...
# A 2024-07-04 1 YZM UN160SM2 ... 23.40 506 3236 12148
# A 2024-07-04 3 YZM UN160SM2 ... 15.80 612 2448 120000
# A 2024-07-04 5 YZM UN160A ... 2.85 2500 4800 2205
# A 2024-07-04 7 NaN UN120A ... 2.40 3500 8500 31244
# A 2024-07-04 8 ZD EM150-V ... 4.60 3000 2800 417
#
# [5 rows x 15 columns]# 9-4、讀取特定列
# 生產(chǎn)日期 班別 機臺
# 0 2024-07-04 A 1
# 1 2024-07-04 A 3
# 2 2024-07-04 A 5
# 3 2024-07-04 A 7
# 4 2024-07-04 A 8# 9-5、數(shù)據(jù)類型轉(zhuǎn)換
# 生產(chǎn)日期 班別 機臺 設(shè)備品牌 ... 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 0 2024-07-04 00:00:00 A 1 YZM ... 23.40 506 3236 12148
# 1 2024-07-04 00:00:00 A 3 YZM ... 15.80 612 2448 120000
# 2 2024-07-04 00:00:00 A 5 YZM ... 2.85 2500 4800 2205
# 3 2024-07-04 00:00:00 A 7 NaN ... 2.40 3500 8500 31244
# 4 2024-07-04 00:00:00 A 8 ZD ... 4.60 3000 2800 417
#
# [5 rows x 16 columns]# 9-6、使用自定義缺失值
# 生產(chǎn)日期 班別 機臺 設(shè)備品牌 設(shè)備型號 ... 生產(chǎn)周期(s) 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 0 2024-07-04 A 1 YZM UN160SM2 ... 38.0 23.40 506 3236 12148
# 1 2024-07-04 A 3 YZM UN160SM2 ... 38.6 15.80 612 2448 120000
# 2 2024-07-04 A 5 YZM UN160A ... 30.1 2.85 2500 4800 2205
# 3 2024-07-04 A 7 NaN UN120A ... 28.6 2.40 3500 8500 31244
# 4 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 2800 417
#
# [5 rows x 16 columns]# 9-7、跳過行和讀取特定行數(shù)
# 2024-07-04 00:00:00 A 3 YZM UN160SM2 ... 38.6 15.8 612 2448 120000
# 0 2024-07-04 A 5 YZM UN160A ... 30.1 2.85 2500 4800 2205
# 1 2024-07-04 A 7 NaN UN120A ... 28.6 2.40 3500 8500 31244
# 2 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 2800 417
# 3 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 3000 312
# 4 2024-07-04 A 12 HT HA2600 ... 23.2 8.80 1000 14500 143100
#
# [5 rows x 16 columns]# 9-8、日期解析
# 生產(chǎn)日期 班別 機臺 設(shè)備品牌 設(shè)備型號 ... 生產(chǎn)周期(s) 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 0 2024-07-04 A 1 YZM UN160SM2 ... 38.0 23.40 506 3236 12148
# 1 2024-07-04 A 3 YZM UN160SM2 ... 38.6 15.80 612 2448 120000
# 2 2024-07-04 A 5 YZM UN160A ... 30.1 2.85 2500 4800 2205
# 3 2024-07-04 A 7 NaN UN120A ... 28.6 2.40 3500 8500 31244
# 4 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 2800 417
#
# [5 rows x 16 columns]# 9-9、讀取尾部行
# 生產(chǎn)日期 班別 機臺 設(shè)備品牌 設(shè)備型號 ... 生產(chǎn)周期(s) 單重(g) 包裝規(guī)格 當班產(chǎn)量(pc) 當日庫存(pc)
# 0 2024-07-04 A 1 YZM UN160SM2 ... 38.0 23.40 506 3236 12148
# 1 2024-07-04 A 3 YZM UN160SM2 ... 38.6 15.80 612 2448 120000
# 2 2024-07-04 A 5 YZM UN160A ... 30.1 2.85 2500 4800 2205
# 3 2024-07-04 A 7 NaN UN120A ... 28.6 2.40 3500 8500 31244
# 4 2024-07-04 A 8 ZD EM150-V ... 33.0 4.60 3000 2800 417
#
# [5 rows x 16 columns]