版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
大數(shù)據(jù)分析工具Pandas用法目錄TOC\o"1-2"\h\u12478一、Pandas系列 3293121.從List創(chuàng)建Series 4151022.在Series中添加相應(yīng)的索引 571413.創(chuàng)建一個(gè)基本系列,是一個(gè)空系列 5243184.從ndarray創(chuàng)建一個(gè)系列 5259755.從字典創(chuàng)建一個(gè)系列 679826.從標(biāo)量創(chuàng)建一個(gè)系列 654567.從具有位置的系列中訪問數(shù)據(jù) 7268868.使用標(biāo)簽檢索數(shù)據(jù)(索引) 711204二、Pandas數(shù)據(jù)幀 7187759.創(chuàng)建一個(gè)空的數(shù)據(jù)幀 8842410.從列表創(chuàng)建數(shù)據(jù)幀 8928811.從ndarrays/Lists的字典創(chuàng)建數(shù)據(jù)幀 92742412.從字典列表創(chuàng)建數(shù)據(jù)幀 9566013.從系列的字典創(chuàng)建數(shù)據(jù)幀 103259114.列選擇 10286015.行選擇 1116228三、Pandas面板 112067416.pandas.Panel() 111169817.從3Dndarray創(chuàng)建面板 123087818.從數(shù)據(jù)幀對象的dict創(chuàng)建面板 121867019.創(chuàng)建一個(gè)空面板 131932120.從面板中選擇數(shù)據(jù) 135465四、Pandas快速入門 15326121對象創(chuàng)建 15301272查看數(shù)據(jù) 1749233選擇區(qū)塊 208255五、總結(jié) 29Pandas是Python的一個(gè)數(shù)據(jù)分析包,最初由AQRCapitalManagement于2008年4月開發(fā),并于2009年底開源出來,目前由專注于Python數(shù)據(jù)包開發(fā)的PyData開發(fā)團(tuán)隊(duì)繼續(xù)開發(fā)和維護(hù),屬于PyData項(xiàng)目的一部分。Pandas最初被作為金融數(shù)據(jù)分析工具而開發(fā)出來,因此Pandas為時(shí)間序列分析提供了很好的支持。Pandas的名稱來自于面板數(shù)據(jù)(PanelData)和Python數(shù)據(jù)分析(Data Analysis)。面板數(shù)據(jù)是經(jīng)濟(jì)學(xué)中關(guān)于多維數(shù)據(jù)集的一個(gè)術(shù)語,在Pandas中提供了Panel的數(shù)據(jù)類型。標(biāo)準(zhǔn)的Python發(fā)行版并沒有將Pandas模塊捆綁在一起發(fā)布。安裝Pandas模塊的一個(gè)輕量級的替代方法是使用流行的Python包安裝程序用pip來安裝Pandas。Pandas在Python上的安裝同樣使用pip進(jìn)行:pipinstallpandas。Pandas處理以下3種數(shù)據(jù)結(jié)構(gòu):系列(Series)。數(shù)據(jù)幀(DataFrame)。面板(Panel)。Pandas系列是具有均勻數(shù)據(jù)的一維數(shù)組結(jié)構(gòu)。Series像Python中的數(shù)據(jù)類型List一樣,每個(gè)數(shù)據(jù)都有自己的索引。系列可以使用以下構(gòu)造函數(shù)創(chuàng)建:pandas.Series(data,index,dtype,copy)data:數(shù)據(jù)采取各種形式,如ndarray、list、constants。index:索引值必須是唯一的和散列的,與數(shù)據(jù)的長度相同。默認(rèn)為np.arange(n),如果沒有索引被傳遞。dtype:用于數(shù)據(jù)類型。如果沒有,那么將推斷數(shù)據(jù)類型。copy,復(fù)制數(shù)據(jù),默認(rèn)為false。從List創(chuàng)建Series【例6.1】在Series中添加相應(yīng)的索引【例6.2】>>>importnumpyasnp>>>importnumpyasnp>>>ts=pd.Series(np.random.randn(365),index=np.arange(1,366))>>>ts在index中設(shè)置索引值,是一個(gè)從1到366的值。創(chuàng)建一個(gè)基本系列,是一個(gè)空系列【例6.3】#importthepandaslibraryandaliasingaspdimportpandasaspd#importthepandaslibraryandaliasingaspdimportpandasaspds=pd.Series()prints輸出結(jié)果如下: Series([],dtype:float64) 從ndarray創(chuàng)建一個(gè)系列【例6.4】如果數(shù)據(jù)是ndarray,傳遞的索引就必須具有相同的長度。如果沒有傳遞索引值,那么默認(rèn)的索引將是范圍(n),其中n是數(shù)組長度,即[0,1,2,3….range(len(array))-1]-1]。#importthepandaslibraryandaliasingaspdimportpandasaspd#importthepandaslibraryandaliasingaspdimportpandasaspdimportnumpyasnpdata=np.array(['a','b','c','d'])s=pd.Series(data)prints輸出結(jié)果如下:aabcddtype:object這里沒有傳遞任何索引,因此它分配了從0到len(data)-1的索引,即0到3。從字典創(chuàng)建一個(gè)系列【例6.5】字典(Dict)可以作為輸入傳遞,如果沒有指定索引,就按排序順序取得字典鍵以構(gòu)造索引。如果傳遞了索引,索引中與標(biāo)簽對應(yīng)的數(shù)據(jù)中的值就會(huì)被拉出。#importthepandaslibraryandaliasingaspdimportpandasaspd#importthepandaslibraryandaliasingaspdimportpandasaspdimportnumpyasnpdata={'a':0.,'b':1.,'c':2.}s=pd.Series(data)prints輸出結(jié)果如下:aa0.0b1.0c2.0dtype:float64注意:字典鍵用于構(gòu)建索引。從標(biāo)量創(chuàng)建一個(gè)系列【例6.6】如果數(shù)據(jù)是標(biāo)量值,就必須提供索引。將重復(fù)該值以匹配索引的長度。#importthepandaslibraryandaliasingaspdimportpandasaspd#importthepandaslibraryandaliasingaspdimportpandasaspdimportnumpyasnps=pd.Series(5,index=[0,1,2,3])prints輸出結(jié)果如下:005152535dtype:int64從具有位置的系列中訪問數(shù)據(jù)【例6.7】系列中的數(shù)據(jù)可以類似于訪問ndarray中的數(shù)據(jù)來訪問。檢索第一個(gè)元素。比如已經(jīng)知道數(shù)組從0開始計(jì)數(shù),第一個(gè)元素存儲(chǔ)在0位置等。importpandasaspdimportpandasaspds=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])#retrievethefirstelementprints[0]得到以下結(jié)果: 1 使用標(biāo)簽檢索數(shù)據(jù)(索引)【例6.8】一個(gè)系列就像一個(gè)固定大小的字典,可以通過索引標(biāo)簽獲取和設(shè)置值。使用索引標(biāo)簽值檢索單個(gè)元素。importpandasaspdimportpandasaspds=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])#retrieveasingleelementprints['a']得到以下結(jié)果: 1 Pandas數(shù)據(jù)幀數(shù)據(jù)幀(DataFrame)是二維數(shù)據(jù)結(jié)構(gòu),即數(shù)據(jù)以行和列的表格方式排列。數(shù)據(jù)幀的功能特點(diǎn)如下:潛在的列是不同的類型。大小可變。標(biāo)記軸(行和列)??梢詫π泻土袌?zhí)行算術(shù)運(yùn)算。Pandas中的DataFrame可以使用以下構(gòu)造函數(shù)創(chuàng)建: pandas.DataFrame(data,index,columns,dtype,copy) data:數(shù)據(jù)采取各種形式,如ndarray、series、map、lists、dict、constant和另一個(gè)DataFrame。index:對于行標(biāo)簽,要用于結(jié)果幀的索引是可選默認(rèn)值np.arrange(n),如果沒有傳遞索引值。columns:對于列標(biāo)簽,可選的默認(rèn)語法是-np.arange(n)。只有在沒有索引傳遞的情況下才是這樣。dtype:每列的數(shù)據(jù)類型。copy:如果默認(rèn)值為False,此命令就用于復(fù)制數(shù)據(jù)。Pandas數(shù)據(jù)幀可以使用各種輸入創(chuàng)建:列表。字典。系列。NumPyndarrays。另一個(gè)數(shù)據(jù)幀。創(chuàng)建一個(gè)空的數(shù)據(jù)幀創(chuàng)建基本數(shù)據(jù)幀,是空數(shù)據(jù)幀?!纠?.9】#importthepandaslibraryandaliasingaspdimportpandasaspd#importthepandaslibraryandaliasingaspdimportpandasaspddf=pd.DataFrame()printdf得到以下結(jié)果:EmptyDataFrameColumns:[]Index:[]EmptyDataFrameColumns:[]Index:[]從列表創(chuàng)建數(shù)據(jù)幀可以使用單個(gè)列表或嵌套多個(gè)創(chuàng)建數(shù)據(jù)幀?!纠?.10】importpandasaspddata=[1,2,3,4,5]importpandasaspddata=[1,2,3,4,5]df=pd.DataFrame(data)printdf得到以下結(jié)果:000112233445從ndarrays/Lists的字典創(chuàng)建數(shù)據(jù)幀所有的ndarrays必須具有相同的長度。如果傳遞了索引(Index),索引的長度就應(yīng)等于數(shù)組的長度。如果沒有傳遞索引,那么默認(rèn)情況下,索引將為range(n),其中n為數(shù)組長度?!纠?.11】importpandasaspdimportpandasaspddata={'Name':['Tom','Jack','Steve','Ricky'],'Age':[28,34,29,42]}df=pd.DataFrame(data)printdf得到以下結(jié)果:注意:觀察值0、1、2、3,它們是分配給每個(gè)使用函數(shù)range(n)的默認(rèn)索引。從字典列表創(chuàng)建數(shù)據(jù)幀字典列表可作為輸入數(shù)據(jù)傳遞以用來創(chuàng)建數(shù)據(jù)幀,字典鍵默認(rèn)為列名。【例6.12】以下代碼顯示如何通過傳遞字典列表來創(chuàng)建數(shù)據(jù)幀。importpandasaspdimportpandasaspddata=[{'a':1,'b':2},{'a':5,'b':10,'c':20}]df=pd.DataFrame(data)printdf得到以下結(jié)果:注意:觀察到NaN(不是數(shù)字)被附加在缺失的區(qū)域。從系列的字典創(chuàng)建數(shù)據(jù)幀字典的系列可以傳遞以形成一個(gè)數(shù)據(jù)幀。所得到的索引是通過索引的所有系列的并集?!纠?.13】得到以下結(jié)果:注意:對于第一個(gè)系列,觀察到?jīng)]有傳遞標(biāo)簽'd',但在結(jié)果中,對于d標(biāo)簽附加了NaN。列選擇下面將從數(shù)據(jù)幀中選擇一列?!纠?.14】得到以下結(jié)果:行選擇可以通過將行標(biāo)簽傳遞給loc()函數(shù)來選擇行。【例6.15】得到以下結(jié)果:oneone2.0two2.0Name:b,dtype:float64結(jié)果是一系列標(biāo)簽作為數(shù)據(jù)幀的列名稱。而且,系列的名稱是檢索的標(biāo)簽。Pandas面板面板(Panel)是3D容器的數(shù)據(jù)。面板數(shù)據(jù)一詞來源于計(jì)量經(jīng)濟(jì)學(xué),部分源于名稱:Pandas-pan(el)-da(ta)-s。3個(gè)軸(Axis)這個(gè)名稱旨在給出描述涉及面板數(shù)據(jù)的操作的一些語義。它們是:items-axis0:每個(gè)項(xiàng)目對應(yīng)于內(nèi)部包含的數(shù)據(jù)幀。major_axis-axis1:每個(gè)數(shù)據(jù)幀的索引(行)。minor_axis-axis2:每個(gè)數(shù)據(jù)幀的列。pandas.Panel()使用以下構(gòu)造函數(shù)創(chuàng)建面板:pandas.Panel(data,items,major_axis,minor_axis,dtype,copy)構(gòu)造函數(shù)的參數(shù)說明如下。data:數(shù)據(jù)采取各種形式,如ndarray、series、map、lists、dict、constant和另一個(gè)數(shù)據(jù)幀。items:axis=0。major_axis:axis=1。minor_axis:axis=2。dtype:每列的數(shù)據(jù)類型。copy:復(fù)制數(shù)據(jù),默認(rèn)為false。從3Dndarray創(chuàng)建面板【例6.16】#creatinganemptypanelimportpandasaspdimportnumpyas#creatinganemptypanelimportpandasaspdimportnumpyasnpdata=np.random.rand(2,4,5)p=pd.Panel(data)printp得到以下結(jié)果:<class'pandas.core.panel.Panel'><class'pandas.core.panel.Panel'>Dimensions:2(items)x4(major_axis)x5(minor_axis)Itemsaxis:0to1Major_axisaxis:0to3Minor_axisaxis:0to4注意:觀察空面板和上面板的尺寸大小,所有對象都不同。從數(shù)據(jù)幀對象的dict創(chuàng)建面板【例6.17】#creatinganemptypanelimportpandasaspdimportnumpyasnp#creatinganemptypanelimportpandasaspdimportnumpyasnpdata={'Item1':pd.DataFrame(np.random.randn(4,3)),'Item2':pd.DataFrame(np.random.randn(4,2))}p=pd.Panel(data)printp得到以下結(jié)果:<class'pandas.core.panel.Panel'><class'pandas.core.panel.Panel'>Dimensions:2(items)x4(major_axis)x5(minor_axis)Itemsaxis:0to1Major_axisaxis:0to3Minor_axisaxis:0to4創(chuàng)建一個(gè)空面板可以使用Panel的構(gòu)造函數(shù)創(chuàng)建一個(gè)空面板:【例6.18】#creatinganemptypanelimportpandasaspd#creatinganemptypanelimportpandasaspdp=pd.Panel()printp得到以下結(jié)果:<class'pandas.core.panel.Panel'><class'pandas.core.panel.Panel'>Dimensions:0(items)x0(major_axis)x0(minor_axis)Itemsaxis:NoneMajor_axisaxis:NoneMinor_axisaxis:None從面板中選擇數(shù)據(jù)要從面板中選擇數(shù)據(jù),可以使用以下方式:Items。Major_axis。Minor_axis。使用Items【例6.19】#creatinganemptypanelimportpandasaspdimportnumpyas#creatinganemptypanelimportpandasaspdimportnumpyasnpdata={'Item1':pd.DataFrame(np.random.randn(4,3)),'Item2':pd.DataFrame(np.random.randn(4,2))}p=pd.Panel(data)printp['Item1']得到以下結(jié)果:上面的示例有兩個(gè)數(shù)據(jù)項(xiàng),這里只檢索item1。結(jié)果是具有4行和3列的數(shù)據(jù)幀,它們是Major_axis和Minor_axis維。使用major_axis可以使用panel.major_axis(index)方法訪問數(shù)據(jù)?!纠?.20】#creatinganemptypanelimportpandasaspdimportnumpyas#creatinganemptypanelimportpandasaspdimportnumpyasnpdata={'Item1':pd.DataFrame(np.random.randn(4,3)),'Item2':pd.DataFrame(np.random.randn(4,2))}p=pd.Panel(data)printp.major_xs(1)得到以下結(jié)果:使用minor_axis使用panel.minor_axis(index)方法訪問數(shù)據(jù)?!纠?.21】#creatinganemptypanelimportpandasaspdimportnumpyas#creatinganemptypanelimportpandasaspdimportnumpyasnpdata={'Item1':pd.DataFrame(np.random.randn(4,3)),'Item2':pd.DataFrame(np.random.randn(4,2))}p=pd.Panel(data)printp.minor_xs(1)得到以下結(jié)果:注意:觀察尺寸大小的變化。Pandas快速入門假設(shè)用戶已安裝Anaconda,現(xiàn)在啟動(dòng)Anaconda開始示例。測試工作環(huán)境是否已安裝Pandas,導(dǎo)入相關(guān)包如下:【例6.22】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpimportmatplotlib.pyplotaspltprint("Hello,Pandas")對象創(chuàng)建通過傳遞值列表來創(chuàng)建一個(gè)系列,讓Pandas創(chuàng)建一個(gè)默認(rèn)的整數(shù)索引?!纠?.23】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnps=pd.Series([1,3,5,np.nan,6,8])print(s)執(zhí)行后輸出結(jié)果如下:通過傳遞NumPy數(shù)組,使用datetime索引和標(biāo)記列來創(chuàng)建數(shù)據(jù)幀。【例6.24】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20180101',periods=7)print(dates)print("--"*16)df=pd.DataFrame(np.random.randn(7,4),index=dates,columns=list('ABCD'))print(df)執(zhí)行后輸出結(jié)果如下:碼:【例6.25】執(zhí)行上面的示例代碼后,輸出結(jié)果如下:查看數(shù)據(jù)查看框架的頂部和底部的數(shù)據(jù)行,參考以下示例代碼:【例6.26】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=7)df=pd.DataFrame(np.random.randn(7,4),index=dates,columns=list('ABCD'))print(df.head())print(" "*print(df.tail(3))執(zhí)行上面的示例代碼后,輸出結(jié)果如下:顯示索引、列和底層NumPy數(shù)據(jù),參考以下代碼:【例6.27】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=7)df=pd.DataFrame(np.random.randn(7,4),index=dates,columns=list('ABCD'))print("indexis:")print(df.index)print("columnsis:")print(df.columns)print("valuesis:")print(df.values)print(df.index)print("columnsis:")print(df.columns)print("valuesis:")print(df.values)執(zhí)行上面的示例代碼后,輸出結(jié)果如下:描述顯示數(shù)據(jù)的快速統(tǒng)計(jì)摘要,參考以下示例代碼:【例6.28】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=7)df=pd.DataFrame(np.random.randn(7,4),index=dates,columns=list('ABCD'))print(df.describe())執(zhí)行上面的示例代碼后,輸出結(jié)果如下:調(diào)換數(shù)據(jù),參考以下示例代碼:【例6.29】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.T)執(zhí)行上面的示例代碼后,輸出結(jié)果如下:通過軸排序,參考以下示例代碼:【例6.30】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.sort_index(axis=1,ascending=False))執(zhí)行上面的示例代碼后,輸出結(jié)果如下:按值排序,參考以下示例代碼:【例6.31】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.sort_values(by='B'))執(zhí)行上面的示例代碼后,輸出結(jié)果如下:選擇區(qū)塊注意,雖然用于選擇和設(shè)置的標(biāo)準(zhǔn)Python/NumPy表達(dá)式是直觀的,可用于交互式工作,但對于生產(chǎn)代碼,建議使用優(yōu)化的Pandas數(shù)據(jù)訪問方法.at、.iat、.loc、.iloc和.ix。獲取①選擇一列,產(chǎn)生一個(gè)系列,相當(dāng)于df.A,參考以下示例代碼:【例6.32】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df['A'])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:runfile('C:/Users/DLG/.spyder-py3/temp.py',wdir='C:/Users/DLG/.spyder-py3')runfile('C:/Users/DLG/.spyder-py3/temp.py',wdir='C:/Users/DLG/.spyder-py3')2018-11-16 -1.0740022018-11-17 1.4407502018-11-18 1.8123732018-11-19 0.8770452018-11-20 0.3139872018-11-21 0.425046Freq:D,Name:A,dtype:float64②通過[]操作符選擇切片行,參考以下示例代碼:【例6.33】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df[0:3])print("=========指定選擇日期========")print(df['20181116':'20181117'])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:按標(biāo)簽選擇①使用標(biāo)簽獲取橫截面,參考以下示例代碼:【例6.34】 importpandasaspd importnumpyasnpimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.loc[dates[0]])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:②通過標(biāo)簽選擇多軸,參考以下示例代碼:【例6.35】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.loc[:,['A','B']])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:③顯示標(biāo)簽切片,包括兩個(gè)端點(diǎn),參考以下示例代碼:【例6.36】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.loc['20181116':'20181117',['A','B']])print(df.loc['20181116':'20181117',['A','B']])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:④減少返回對象的尺寸(大小),參考以下示例代碼:【例6.37】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20170101',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.loc['20181116',['A','B']])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:⑤獲得標(biāo)量值,參考以下示例代碼:【例6.38】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.loc[dates[0],'A'])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:runfile('C:/Users/DLG/.spyder-py3/temp.py',wdir='C:/Users/DLG/.spyder-py3')runfile('C:/Users/DLG/.spyder-py3/temp.py',wdir='C:/Users/DLG/.spyder-py3')-1.709403357791696⑥快速訪問標(biāo)量(等同于先前的方法),參考以下示例代碼:【例6.39】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.at[dates[0],'A'])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:runfile('C:/Users/DLG/.spyder-py3/temp.py',wdir='C:/Users/DLG/.spyder-py3')runfile('C:/Users/DLG/.spyder-py3/temp.py',wdir='C:/Users/DLG/.spyder-py3')-0.38763045860339174通過位置選擇①通過傳遞的整數(shù)的位置選擇,參考以下示例代碼:【例6.40】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.iloc[3])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:②通過整數(shù)切片,類似于NumPy/Python,參考以下示例代碼:【例6.41】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.iloc[3:5,0:2])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:③整數(shù)位置的列表,類似于NumPy/Python樣式,參考以下示例代碼:【例6.42】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.iloc[[1,2,4],[0,2]])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:④明確切片行,參考以下示例代碼:【例6.43】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.iloc[1:3,:])執(zhí)行上面的示例代碼后,輸出結(jié)果如下:⑤明確切片列,參考以下示例代碼:【例6.44】importpandasaspdimportnumpyasnpimportpandasaspdimportnumpyasnpdates=pd.date_range('20181116',periods=6)df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))print(df.iloc[:,1:3])執(zhí)行上面的示例代碼后,輸
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025廣告合作經(jīng)營合同
- 商業(yè)計(jì)劃書撰寫與助學(xué)貸款申請技巧
- 課題申報(bào)參考:流域與特殊地理區(qū)域生態(tài)環(huán)境保護(hù)法律問題研究
- 科技醫(yī)療的發(fā)展趨勢及挑戰(zhàn)
- 未來工作趨勢與職業(yè)路徑規(guī)劃的思考
- 室內(nèi)模擬射擊與射箭場設(shè)備出租考核試卷
- 2025年新世紀(jì)版九年級歷史下冊階段測試試卷含答案
- 2025年湘師大新版八年級地理下冊月考試卷含答案
- 2025年新世紀(jì)版選修6歷史下冊月考試卷含答案
- 2025年人民版必修3歷史下冊月考試卷含答案
- 課題申報(bào)書:GenAI賦能新質(zhì)人才培養(yǎng)的生成式學(xué)習(xí)設(shè)計(jì)研究
- 2024年江蘇省中醫(yī)院高層次衛(wèi)技人才招聘筆試歷年參考題庫頻考點(diǎn)附帶答案
- 駱駝祥子-(一)-劇本
- 全國醫(yī)院數(shù)量統(tǒng)計(jì)
- 《中國香文化》課件
- 2024年醫(yī)美行業(yè)社媒平臺(tái)人群趨勢洞察報(bào)告-醫(yī)美行業(yè)觀察星秀傳媒
- 第六次全國幽門螺桿菌感染處理共識報(bào)告-
- 天津市2023-2024學(xué)年七年級上學(xué)期期末考試數(shù)學(xué)試題(含答案)
- 經(jīng)濟(jì)學(xué)的思維方式(第13版)
- 盤錦市重點(diǎn)中學(xué)2024年中考英語全真模擬試卷含答案
- 背景調(diào)查報(bào)告
評論
0/150
提交評論