假设excel已有一列数据形如A列,现需要在B列追加A列中每个数字出现的个数
引入对应模块
import matplotlib.pyplot as plt
import xlrd
import xlwt
import xlutils.copy
from collections import defaultdict
代码
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
savepath = "xlsx/rating.xlsx"
def save_data(savepath):
data = xlrd.open_workbook('xlsx/rating.xlsx')
table = data.sheets()[0]
t1 = table.col_values(0)
# book = xlwt.Workbook(encoding='utf-8', style_compression=0)
# sheet = book.add_sheet('result', cell_overwrite_ok=True)
ws = xlutils.copy.copy(data) # 复制之前表里存在的数据
table = ws.get_sheet(0)
for i in range(len(t1)):
t = t1.count(t1[i])
table.write(i, 1, t)
ws.save(savepath)
save_data(savepath)