python基礎(chǔ)知識之索引與切片詳解_第1頁
python基礎(chǔ)知識之索引與切片詳解_第2頁
python基礎(chǔ)知識之索引與切片詳解_第3頁
python基礎(chǔ)知識之索引與切片詳解_第4頁
python基礎(chǔ)知識之索引與切片詳解_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第python基礎(chǔ)知識之索引與切片詳解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#易錯點(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開始的索引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'#按照label索引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:[]

#按照label索引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是什么類型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')

#此處沒有報錯,千萬注意index類型In[176]:df.loc['2']Out[176]:

a1b4Name:2,dtype:int64

#ix是一個功能強(qiáng)大的函數(shù),但是爭議卻很大,往往是錯誤之源

#咦,怎么輸出與預(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

#注意開閉區(qū)間In[180]:df.loc['2':'3']Out[180]:

ab214325

總結(jié)

pandas中ix是錯誤之源,大型項(xiàng)目大量使用它時,往往造成不可預(yù)料的后果。0.20.x版本也標(biāo)記為拋棄該函數(shù),二義性和[]區(qū)間,違背Exp

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論