写在前面的话: 很久之前同学要我改个excel的数据,把所有大于30000的数据变为0,但是又不会操作excel(一个文件中包含多个sheet,我也不会~),所以只能用python做数据处理了,查了一下并简单应用,记录一下,以后查阅方便。
最近实习了,之后找点时间把问题总结总结,还有把毕设和之前的图像处理都总结一下,发现自己真的好菜,要补的太多了T_T
用 pandas 进行处理因为要将第7,8,9列的大于30000的数据变为0,所以需要对这三列进行处理。过程中会遇到报错信息,没有安装什么库安装即可,一般有 xlrd, openpyxl
你要怎么处理就改处理的方法,然后对应数据 apply 对应的 方法 就可以了 #!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @File : process_excel.py @Time : 2020/02/14 10:32:03 @Author : 小木 @Contact : hunt_hak@outlook.com ''' # here put the import lib import pandas as pd # import numpy as np import xlrd file_name = './1985.xlsx' save_path = './1986.xlsx' wb = xlrd.open_workbook(file_name) sheets = wb.sheet_names() # 返回excel里sheet的名称 # print(sheets) def fun(x): if x >= 30000: return 0 else: return x write = pd.ExcelWriter(save_path) # 遍历sheet进行处理 for sheet in sheets: data = pd.read_excel(file_name, header=None, sheet_name=sheet) # print(data.head(10)) # print('*' * 40 + "Processing {}...".format(sheet) + '*' * 40) values = data[7].apply(lambda x: fun(x)) data[7] = values values = data[8].apply(lambda x: fun(x)) data[8] = values values = data[9].apply(lambda x: fun(x)) data[9] = values # print(data.head(10)) data.to_excel(write, sheet_name=sheet, index=False, header=None) write.save() # print('*' * 40 + '{} has been saved'.format(sheet) + '*' * 40 + '\n') write.close()