




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第python基礎(chǔ)知識(shí)之索引與切片詳解In[63]:arr[:,-10]---------------------------------------------------------------------------IndexErrorTraceback(mostrecentcalllast)
ipython-input-63-2ffa6627dc7finmodule()----1arr[:,-10]IndexError:index-10isoutofboundsforaxis1withsize5
numpy.array索引三維N維
In[67]:importnumpyasnp
In[68]:arr=np.arange(30).reshape(2,3,5)
In[69]:arr
Out[69]:
array([[[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14]],[[15,16,17,18,19],
[20,21,22,23,24],
[25,26,27,28,29]]])
#根據(jù)axis=0選取
In[70]:arr[0]
Out[70]:
array([[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14]])
In[71]:arr[1]
Out[71]:
array([[15,16,17,18,19],
[20,21,22,23,24],
[25,26,27,28,29]])
#根據(jù)axis=1選取
In[72]:arr[:,0]
Out[72]:
array([[0,1,2,3,4],
[15,16,17,18,19]])
In[73]:arr[:,1]
Out[73]:
array([[5,6,7,8,9],
[20,21,22,23,24]])
#異常指出axis=1超出范圍
In[74]:arr[:,4]---------------------------------------------------------------------------IndexErrorTraceback(mostrecentcalllast)
ipython-input-74-9d489478e7c7inmodule()----1arr[:,4]IndexError:index4isoutofboundsforaxis1withsize3#根據(jù)axis=2選取
In[75]:arr[:,:,0]
Out[75]:
array([[0,5,10],
[15,20,25]])
In[76]:arr[:,:,0].shape
Out[76]:(2,3)
In[78]:arr[:,:,0:2]
Out[78]:
array([[[0,1],
[5,6],
[10,11]],[[15,16],
[20,21],
[25,26]]])
In[79]:arr[:,:,0:2].shape
Out[79]:(2,3,2)
#左/右側(cè)超出范圍
In[81]:arr[:,:,0:100]
Out[81]:
array([[[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14]],[[15,16,17,18,19],
[20,21,22,23,24],
[25,26,27,28,29]]])
#異常axis=0In[82]:arr[100,:,0:100]---------------------------------------------------------------------------IndexErrorTraceback(mostrecentcalllast)
ipython-input-82-21efcc74439dinmodule()----1arr[100,:,0:100]IndexError:index100isoutofboundsforaxis0withsize2
pandasSeries索引
In[84]:s=pd.Series(['You','are','a','nice','girl'])In[85]:sOut[85]:0You1are2a3nice4girl
dtype:object#按照索引選擇In[86]:s[0]Out[86]:'You'#[]In[87]:s[0:0]Out[87]:Series([],dtype:object)In[88]:s[0:-1]Out[88]:0You1are2a3nice
dtype:object#易錯(cuò)點(diǎn),ix包含區(qū)間為[]In[91]:s.ix[0:0]Out[91]:0You
dtype:objectIn[92]:s.ix[0:1]Out[92]:0You1are
dtype:object#ix索引不存在indexIn[95]:s.ix[400]
KeyError:400#按照從0開(kāi)始的索引In[95]:s.iloc[0]Out[95]:'You'In[96]:s.iloc[1]Out[96]:'are'In[97]:s.iloc[100]
IndexError:singlepositionalindexerisout-of-boundsIn[98]:s=pd.Series(['You','are','a','nice','girl'],index=list('abcde'))In[99]:sOut[99]:
aYou
bare
dnice
egirl
dtype:objectIn[100]:s.iloc[0]Out[100]:'You'In[101]:s.iloc[1]Out[101]:'are'#按照l(shuí)abel索引In[103]:s.loc['a']Out[103]:'You'In[104]:s.loc['b']Out[104]:'are'In[105]:s.loc[['b','a']]Out[105]:
bare
aYou
dtype:object#loc切片索引In[106]:s.loc['a':'c']Out[106]:
aYou
bare
dtype:objectIn[108]:s.indexOut[108]:Index(['a','b','c','d','e'],dtype='object')
pandasDataFrame索引
In[114]:importpandasaspdIn[115]:df=pd.DataFrame({'open':[1,2,3],'high':[4,5,6],'low':[6,3,1]},index=pd.period_range('30/12/2017',perio
...:ds=3,freq='H'))In[116]:dfOut[116]:
highlowopen2017-12-3000:004612017-12-3001:005322017-12-3002:00613#按列索引In[117]:df['high']Out[117]:2017-12-3000:0042017-12-3001:0052017-12-3002:006Freq:H,Name:high,dtype:int64In[118]:df.highOut[118]:2017-12-3000:0042017-12-3001:0052017-12-3002:006Freq:H,Name:high,dtype:int64In[120]:df[['high','open']]Out[120]:
highopen2017-12-3000:00412017-12-3001:00522017-12-3002:0063In[122]:df.ix[:]
D:\CodeTool\Python\Python36\Scripts\ipython:1:DeprecationWarning:
.ixisdeprecated.Pleaseuse
.locforlabelbasedindexingor.ilocforpositionalindexingIn[123]:df.iloc[0:0]Out[123]:EmptyDataFrame
Columns:[high,low,open]Index:[]In[124]:df.ix[0:0]Out[124]:EmptyDataFrame
Columns:[high,low,open]Index:[]
#按照l(shuí)abel索引In[127]:df.indexOut[127]:PeriodIndex(['2017-12-3000:00','2017-12-3001:00','2017-12-3002:00'],dtype='period[H]',freq='H')In[128]:df.loc['2017-12-3000:00']Out[128]:
high4low6open1Name:2017-12-3000:00,dtype:int64
#檢查參數(shù)In[155]:df.loc['2017-12-3000:00:11']Out[155]:
high4low6open1Name:2017-12-3000:00,dtype:int64In[156]:df.loc['2017-12-3000:00:66']
KeyError:'thelabel[2017-12-3000:00:66]isnotinthe[index]'
填坑
In[158]:df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6]},index=[2,3,4])In[159]:dfOut[159]:
ab214325436#iloc取第一行正確用法In[160]:df.iloc[0]Out[160]:
a1b4Name:2,dtype:int64
#loc正確用法In[165]:df.loc[[2,3]]Out[165]:
ab214325#注意此處index是什么類(lèi)型In[167]:df.loc['2']
KeyError:'thelabel[2]isnotinthe[index]'#索引Int64IndexOut[172]:Int64Index([2,3,4],dtype='int64')
#索引為字符串In[168]:df=pd.DataFrame({'a':[1,2,3],'b':[4,5,6]},index=list('234'))In[169]:dfOut[169]:
ab214325436In[170]:df.indexOut[170]:Index(['2','3','4'],dtype='object')
#此處沒(méi)有報(bào)錯(cuò),千萬(wàn)注意index類(lèi)型In[176]:df.loc['2']Out[176]:
a1b4Name:2,dtype:int64
#ix是一個(gè)功能強(qiáng)大的函數(shù),但是爭(zhēng)議卻很大,往往是錯(cuò)誤之源
#咦,怎么輸出與預(yù)想不一致!In[177]:df.ix[2]
D:\CodeTool\Python\Python36\Scripts\ipython:1:DeprecationWarning:
.ixisdeprecated.Pleaseuse
.locforlabelbasedindexingor.ilocforpositionalindexing
Seethedocumentationhere:
/pandas-docs/stable/indexing.html#ix-indexer-is-deprecatedOut[177]:
a3b6Name:4,dtype:int64
#注意開(kāi)閉區(qū)間In[180]:df.loc['2':'3']Out[180]:
ab214325
總結(jié)
pandas中ix是錯(cuò)誤之源,大型項(xiàng)目大量使用它時(shí),往往造成不可預(yù)料的后果。0.20.x版本也標(biāo)記為拋棄該函數(shù),二義性和[]區(qū)間,違背Exp
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業(yè)商標(biāo)管理培訓(xùn)
- 中石油冬季“九防”專(zhuān)題安全培訓(xùn)
- 護(hù)理查房病例討論制度
- 推進(jìn)江河保護(hù)治理實(shí)施方案
- 廈門(mén)城市職業(yè)學(xué)院《歌劇與音樂(lè)劇排練1》2023-2024學(xué)年第一學(xué)期期末試卷
- 神經(jīng)內(nèi)科腦梗護(hù)理小講課
- 西安電子科技大學(xué)《交響音樂(lè)賞析》2023-2024學(xué)年第一學(xué)期期末試卷
- 江蘇電子信息職業(yè)學(xué)院《人體運(yùn)動(dòng)學(xué)》2023-2024學(xué)年第一學(xué)期期末試卷
- 廣東女子職業(yè)技術(shù)學(xué)院《相對(duì)論簡(jiǎn)介》2023-2024學(xué)年第一學(xué)期期末試卷
- 黔西南民族職業(yè)技術(shù)學(xué)院《中國(guó)古代文學(xué)作品選先秦至唐五代》2023-2024學(xué)年第一學(xué)期期末試卷
- GB/T 43543-2023漱口水
- 鋼廠燒結(jié)機(jī)安裝施工組織設(shè)計(jì)
- 國(guó)家開(kāi)放大學(xué)電大專(zhuān)科《憲法學(xué)》2025期末試題及答案
- 電信營(yíng)業(yè)廳規(guī)章制度范文(2篇)
- 火龍罐療法經(jīng)典課件
- 德國(guó)司法鑒定培訓(xùn)心得
- xxxx智能化工程施工進(jìn)度計(jì)劃表
- 汽車(chē)修理廠管理制度
- 孫正聿《哲學(xué)通論》(修訂版)配套題庫(kù)【考研真題精選+專(zhuān)項(xiàng)題庫(kù)】
- 2023無(wú)損檢測(cè)技術(shù)資格人員考試泄漏檢測(cè)試卷(練習(xí)題庫(kù))
- 國(guó)開(kāi)電大本科《理工英語(yǔ)4》機(jī)考總題庫(kù)
評(píng)論
0/150
提交評(píng)論