import numpy as np import pandas as pd def read_asc(filepath): usecols = [0, 4] # 仅读取第0列和第4列 chunksize = 100000 # 每次读取100000行数据 for chunk in pd.read_csv(filepath, skiprows=4, encoding="gbk", engine='python', sep=' ', delimiter=None, index_col=False, header=None, skipinitialspace=True, usecols=usecols, chunksize=chunksize): file = np.array(chunk) data = read_message(file) filter_step_size(data) def read_message(file): mask = file[:, 1] == "107" data = file[mask] return data def filter_step_size(data): diff = np.diff(data[:, 0].astype(float)) # 将第0列转换为浮点数类型 mask = np.logical_and(0.090 < diff, diff < 0.110) success_sum = np.count_nonzero(mask) fail_sum = len(mask) - success_sum result = np.column_stack((data[:-1, 0], data[1:, 0], diff)) result = result print("步长通过数: {}".format(success_sum)) print("步长未通过数: {}".format(fail_sum)) print("未通过前一项值:未通过后一项值:差值:") print(result) if name == 'main': read_asc("E:\package\databin 7-12-2023 2-23-05 pm Messages File.asc") 修改这段代码,循环读取xlsx文件中第三列除去第一行的所有数据,将读取的数据依次赋值给read_message方法中的mask,并执行整段代码,每次赋值后打印的结果能明显区分
时间: 2024-04-14 15:31:49 浏览: 108
python学习教程之Numpy和Pandas的使用
import numpy as np
import pandas as pd
def read_xlsx(filepath):
df = pd.read_excel(filepath)
data = df.iloc[1:, 2].values
for value in data:
mask = read_message(value)
filter_step_size(mask)
def read_message(value):
mask = value == "107"
return mask
def filter_step_size(mask):
success_sum = np.count_nonzero(mask)
fail_sum = len(mask) - success_sum
result = np.column_stack((mask[:-1], mask[1:]))
result = result.astype(int)
print("步长通过数: {}".format(success_sum))
print("步长未通过数: {}".format(fail_sum))
print("未通过前一项值:未通过后一项值:")
print(result)
if __name__ == '__main__':
read_xlsx("E:\package\databin 7-12-2023 2-23-05 pm Messages File.xlsx")
阅读全文