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

提交评论