您现在的位置是:网站首页> 编程资料编程资料

python使用pandas读xlsx文件的实现_python_

2023-05-26 307人已围观

简介 python使用pandas读xlsx文件的实现_python_

使用pandas读xlsx文件

  • 读取前n行数据
  • 读取指定数据(指定行指定列)
  • 获取文件行号和列标题
  • 将数据转换为字典形式
import pandas as pd #1.读取前n行所有数据 df1=pd.read_excel('d1.xlsx')#读取xlsx中的第一个sheet data1=df1.head(10)#读取前10行所有数据 data2=df1.values#list【】 相当于一个矩阵,以行为单位 #data2=df.values() 报错:TypeError: 'numpy.ndarray' object is not callable print("获取到所有的值:\n{0}".format(data1))#格式化输出 print("获取到所有的值:\n{0}".format(data2)) #2.读取特定行特定列 data3=df1.iloc[0].values#读取第一行所有数据 data4=df1.iloc[1,1]#读取指定行列位置数据:读取(1,1)位置的数据 data5=df1.iloc[[1,2]].values#读取指定多行:读取第一行和第二行所有数据 data6=df1.iloc[:,[0]].values#读取指定列的所有行数据:读取第一列所有数据 print("数据:\n{0}".format(data3)) print("数据:\n{0}".format(data4)) print("数据:\n{0}".format(data5)) print("数据:\n{0}".format(data6)) #3.获取xlsx文件行号、列号 print("输出行号列表{}".format(df1.index.values))#获取所有行的编号:0、1、2、3、4 print("输出列标题{}".format(df1.columns.values))#也就是每列的第一个元素 #4.将xlsx数据转换为字典 data=[] for i in df1.index.values:#获取行号的索引,并对其遍历 #根据i来获取每一行指定的数据,并用to_dict转成字典 row_data=df1.loc[i,['id','name','class','data','score',]].to_dict() data.append(row_data) print("最终获取到的数据是:{0}".format(data)) #iloc和loc的区别:iloc根据行号来索引,loc根据index来索引。 #所以1,2,3应该用iloc,4应该有loc

数据:d1.xlsx

idnameclassdatascore
201901A1Jan-201.3
201902B2Mar-203.4
201903C3May-203.4
201904D1Jan-203.4
201905E1Feb-205.6
201906F1Mar-204.6
201907G1Feb-197.8
201908H2Apr-305.6
201909I3Jan-425.6
201910G4Mar-304.5
201911K5Apr-203.4
201912L6Apr-202.3
201913M4Mar-202.4

运行结果展示

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

-六神源码网