Python數(shù)據(jù)分析基礎(chǔ)與應(yīng)用電子活頁(yè)4-3使用列索引操作DataFrame_第1頁(yè)
Python數(shù)據(jù)分析基礎(chǔ)與應(yīng)用電子活頁(yè)4-3使用列索引操作DataFrame_第2頁(yè)
Python數(shù)據(jù)分析基礎(chǔ)與應(yīng)用電子活頁(yè)4-3使用列索引操作DataFrame_第3頁(yè)
Python數(shù)據(jù)分析基礎(chǔ)與應(yīng)用電子活頁(yè)4-3使用列索引操作DataFrame_第4頁(yè)
Python數(shù)據(jù)分析基礎(chǔ)與應(yīng)用電子活頁(yè)4-3使用列索引操作DataFrame_第5頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

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

文檔簡(jiǎn)介

Python數(shù)據(jù)分析基礎(chǔ)與應(yīng)用模塊電子活頁(yè)4-3使用行索引操作DataFrame【技能訓(xùn)練4-7】使用行索引操作DataFrame【訓(xùn)練要求】?!緦?shí)施過(guò)程】1.使用loc[]屬性基于標(biāo)簽索引選取DataFrame中的數(shù)據(jù)(1)選取DataFrame中的單行數(shù)據(jù)代碼如下:importpandasaspddata1={'name':pd.Series(['安靜','路遠(yuǎn)','溫暖','向北'],index=['stu1','stu2','stu3','stu4']),'age':pd.Series([21,20,19,22],index=['stu1','stu2','stu3','stu4']),'score':pd.Series([86.0,82.5,95.0,90.5],index=['stu1','stu2','stu3','stu4'])}df1=pd.DataFrame(data1)print(df1)輸出結(jié)果:nameagescorestu1安靜2186.0stu2路遠(yuǎn)2082.5stu3溫暖1995.0stu4向北2290.5代碼如下:#選取1行數(shù)據(jù)print(df1.loc['stu2'])輸出結(jié)果:name路遠(yuǎn)age20score82.5Name:stu2,dtype:object(2)選取DataFrame中的多行數(shù)據(jù)代碼如下:importpandasaspddata2={'name':['安靜','路遠(yuǎn)','溫暖','向北'],'age':[21,20,19,22],'score':[86.0,82.5,95.0,90.5]}label1=['stu1','stu2','stu3','stu4']df1=pd.DataFrame(data2,index=label1)#對(duì)多行進(jìn)行操作print(df1.loc['stu1':'stu3',:])#輸出結(jié)果與df1.loc['stu1':'stu3']的相同輸出結(jié)果:nameagescorestu1安靜2186.0stu2路遠(yuǎn)2082.5stu3溫暖1995.0(3)選取DataFrame中的多列數(shù)據(jù)代碼如下:print(df1.loc[:,['name','score']])輸出結(jié)果:namescorestu1安靜86.0stu2路遠(yuǎn)82.5stu3溫暖95.0stu4向北90.5(4)選取DataFrame中的部分行的部分列數(shù)據(jù)代碼如下:print(df1.loc[['stu1','stu3'],['name','score']])輸出結(jié)果:namescorestu1安靜86.0stu3溫暖95.0代碼如下:data3=[['安靜','女',21,86.0],['路遠(yuǎn)','男',20,82.5],['溫暖','男',19,95.0],['向北','女',22,90.5]]df2=pd.DataFrame(data3,index=['stu1','stu2','stu3','stu4'],columns=['name','sex','age','score'])print(df2.loc[['stu1','stu3'],['name','score']])輸出結(jié)果:namescorestu1安靜86.0stu3溫暖95.02.使用iloc[]屬性基于整數(shù)索引選取DataFrame中的數(shù)據(jù)(1)選取DataFrame中的單行數(shù)據(jù)通過(guò)將數(shù)據(jù)行的索引傳遞給iloc[]屬性,可以實(shí)現(xiàn)數(shù)據(jù)行選取。代碼如下:print(df2.iloc[2])輸出結(jié)果:name溫暖sex男age19score95.0Name:stu3,dtype:object(2)選取DataFrame中的多行數(shù)據(jù)代碼如下:#選擇行索引值為1、2的兩行數(shù)據(jù)print(df2.iloc[1:3,:])輸出結(jié)果:namesexagescorestu2路遠(yuǎn)男2082.5stu3溫暖男1995.0(3)選取DataFrame中的多列數(shù)據(jù)代碼如下:#選擇列索引值為1、2的兩列數(shù)據(jù)print(df2.iloc[:,1:3])輸出結(jié)果:sexagestu1女21stu2男20stu3男19stu4女22(4)選取DataFrame中的部分行的部分列數(shù)據(jù)代碼如下:#選擇行索引值為1、3,列索引值為0、3的數(shù)據(jù)print(df2.iloc[[1,3],[0,3]])輸出結(jié)果:namescorestu2路遠(yuǎn)82.5stu4向北90.53.通過(guò)切片操作選取DataFrame中的多行數(shù)據(jù)代碼如下:#選擇第2行和第3行數(shù)據(jù)print(df2[1:3])輸出結(jié)果:namesexagescorestu2路遠(yuǎn)男2082.5stu3溫暖男1995.0代碼如下:#選擇第1行和第3行數(shù)據(jù)print(df2.iloc[0:4:2])輸出結(jié)果:namesexagescorestu1安靜女2186.0stu3溫暖男1995.0【說(shuō)明】iloc[0:4:2]屬性中數(shù)組切片0:4:2表示起始值是0,終止值是4(不包含4),步長(zhǎng)是2,所取數(shù)據(jù)的行索引值分別為0和2。4.DataFrame對(duì)象添加數(shù)據(jù)行(1)使用loc[]屬性結(jié)合標(biāo)簽索引添加數(shù)據(jù)行代碼如下:importpandasaspddf3=pd.DataFrame([['安靜','女',21,86.0],['路遠(yuǎn)','男',20,82.5]],columns=['name','sex','age','score'],index=['stu1','stu2'])df3.loc['stu3']=['溫暖','男',19,95.0]print(df3)輸出結(jié)果:namesexagescorestu1安靜女2186.0stu2路遠(yuǎn)男2082.5stu3溫暖男1995.0(2)使用iloc[]屬性結(jié)合整數(shù)索引添加數(shù)據(jù)行代碼如下:df3.iloc[2]=['溫暖','男',19,95.0]print(df3)輸出結(jié)果:namesexagescorestu1安靜女2186.0stu2路遠(yuǎn)男2082.5stu3溫暖男1995.0(3)使用append()函數(shù)將新的數(shù)據(jù)行添加到DataFrame對(duì)象中代碼如下:importpandasaspddf4=pd.DataFrame([['安靜','女',21,86.0],['路遠(yuǎn)','男',20,82.5]],columns=['name','sex','age','score'])df5=pd.DataFrame([['溫暖','男',19,95.0],['向北','女',22,90.5]],columns=['name','sex','age','score'])#在行末追加新數(shù)據(jù)行df6=df4.append(df5)print(df6)輸出結(jié)果:namesexagescore0安靜女2186.01路遠(yuǎn)男2082.50溫暖男1995.01向北女2290.55.刪除數(shù)據(jù)行(1)使用drop()函數(shù)結(jié)合行索引刪除一行數(shù)據(jù)代碼如下:importpandasaspddata7={'name':['安靜','路遠(yuǎn)','溫暖','向北'],'age':[21,20,19,22],'score':[86.0,82.5,95.0,90.5]}df7=pd.DataFrame(data7)#對(duì)一行進(jìn)行操作df8=df7.drop(0)print(df8)輸出結(jié)果:nameagescore1路遠(yuǎn)2082.52溫暖1995.03向北2290.5上述代碼中,通過(guò)drop(0)刪除了一行數(shù)據(jù)。(2)使用drop()函數(shù)結(jié)合行索引刪除多行數(shù)據(jù)代碼如下:#對(duì)多行進(jìn)行操作df8=df7.drop([0,2])print(df8)輸出結(jié)果:nameagescore1路遠(yuǎn)2082.53向北22

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 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ì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論