Python數據分析基礎與應用電子活頁4-3使用列索引操作DataFrame_第1頁
Python數據分析基礎與應用電子活頁4-3使用列索引操作DataFrame_第2頁
Python數據分析基礎與應用電子活頁4-3使用列索引操作DataFrame_第3頁
Python數據分析基礎與應用電子活頁4-3使用列索引操作DataFrame_第4頁
Python數據分析基礎與應用電子活頁4-3使用列索引操作DataFrame_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

溫馨提示

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

評論

0/150

提交評論