Excel数据分析的代码(包括列表的操作)

import xlrd

xlsx = xlrd.open_workbook('chengji.xls')


# 通过sheet名查找:xlsx.sheet_by_name("sheet1")
# 通过索引查找:xlsx.sheet_by_index(3)
table = xlsx.sheet_by_index(0)

# 获取单个表格值 (2,1)表示获取第3行第2列单元格的值
# value = table.cell_value(2, 1)
# print("第3行2列值为", value)

# 获取表格行数
nrows = table.nrows
print("表格一共有", nrows, "行")

#定义一个字典存放编码类别和次数
all_count=0
code_dict = {"1": 0, "2": 0, "3": 0,"4": 0,"5": 0,"6": 0,"7": 0,"8": 0}
for i in range(1,nrows):
    s=str(table.cell_value(i,2))
    #编码不为空,数量+1
    if s.strip() != '':
        code_dict[s]+=1
        #编码总数量+1
        all_count+=1

改用了pandas,
但是 编码的映射关系没变

python列表
这个里面可以查看每个元素出现的次数,

注意避开两个坑

1 字典的key不要加双引号
2 防止读取数字转成浮点数 读取的时候加参数dtype=object
源代码

import  pandas  as pd

#方法一:默认读取第一个表单
df=pd.read_excel('data/刘老师课堂文本切分的语句3.xlsx',dtype=object)#这个会直接默认读取到这个Excel的第一个表单

i=12
code_1,code_2,code_3=df.iat[i,1],df.iat[i,2],df.iat[i,3]
print(code_1)
print(code_2)
print(code_3)
#定义一个字典存放编码类别和次数
all_count=0
code_dict = {1: 0, 2: 0, 3: 0,4: 0,5: 0,6: 0,7: 0,8: 0,9: 0,10: 0,11: 0}
print(code_dict[code_1])
code_dict[code_1]+=1
print(code_dict[code_1])

#这种是加双引号的遍历

注意最后的结果

1 python 拼接格式化打印
2 python 转百分比的形式现实

print('percent: {:.2%}'.format(42/50))
i='{:.2%}'.format(1/20)
print(type(i))
print(i)

整个项目完整代码和运行截图

在这里插入图片描述

import  pandas  as pd

#方法一:默认读取第一个表单
df=pd.read_excel('data/刘老师课堂文本切分的语句3.xlsx',dtype=object)#这个会直接默认读取到这个Excel的第一个表单
#data=df.head()#默认读取前5行的数据
# data=df.iat[0,1]
# if pd.isna(data):
#     print('1')
# print("获取到所有的值:\n{0}".format(data))#格式化输出


#定义一个字典存放编码类别和次数 以及定义总条数
all_count=len(df)

code_dict = {1: 0, 2: 0, 3: 0,4: 0,5: 0,6: 0,7: 0,8: 0,9: 0,10: 0,11: 0}
#定义列表存放编码类别和顺序,按顺序添加即可记录编码顺序
all_code=[]
for i in df.index.values:#获取行号的索引,并对其进行遍历:
    #遍历每行的三类编码
    print(i)
    code_1,code_2,code_3=df.iat[i,1],df.iat[i,2],df.iat[i,3]
    #因为是三种编码 所以分三种情况
    print(code_1,code_2,code_3)
    #情况一 只有一种编码 权重为一
    if pd.isna(code_2) and pd.isna(code_3):
        print('第一种情况')
        code_1= int(df.iat[i, 1])
        code_dict[code_1]+=1
        all_code.append(code_1)
    #情况二 有两种编码 权重为  7 3
    elif pd.isna(code_3):
        print('第二种情况')
        code_1, code_2= int(df.iat[i, 1]), int(df.iat[i, 2])
        code_dict[code_1]+=0.7
        code_dict[code_2]+=0.3
        all_code.append(code_1)
        all_code.append(code_2)
    #情况三  有三种编码 权重为 5 3 2
    else:
        code_1, code_2, code_3 = int(df.iat[i, 1]), int(df.iat[i, 2]), int(df.iat[i, 3])
        print('第三种情况')
        code_dict[code_1]+=0.5
        code_dict[code_2] += 0.3
        code_dict[code_3] += 0.2
        all_code.append(code_1)
        all_code.append(code_2)
        all_code.append(code_3)

#4.依次打印key和value,通过索引
for key,value in code_dict.items():
    print("类别:%-10s 次数:%-8s 比例:%.4s%%" % (key, value, '{:.2%}'.format(value/all_count)))

print ("列表长度为: " ,len(all_code))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方-教育技术博主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值