编写各种统计类型的函数
1、创建被引用文件:excel_page.py文件,内容如下图,excel表格中要有索引列,自行添加,此处的索引列名称为index
import xlrd
from collections import defaultdict, Counter
class ExcelData:
# 指定excel文件位置及索引列的名字
def __init__(self, xlsx_path, index_col='index'):
workbook = xlrd.open_workbook(xlsx_path)
self.worksheet = workbook.sheet_by_index(0)
self.nrows = self.worksheet.nrows
self.ncols = self.worksheet.ncols
self.index_col = index_col
# 返回对应列的数据为list
def get_col_by_name(self, col_name):
try:
index = self.worksheet.row_values(0).index(col_name)
except BaseException:
return None
# 返回列list,去除列名
return self.worksheet.col_values(index)[1:]
# 对指定的列进行数据计数及统计
def col_count(self, col_name):
col_values = self.get_col_by_name(col_name)
if not col_values:
r = None
else: