import numpy as np import pandas as pd def read_asc(filepath): usecols = [0, 3, 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 = np.logical_and(file[:, 3] == "Rx", file[:, 4] == "1a7") 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[~mask] print("报文通过数: {}".format(success_sum)) print("报文未通过数: {}".format(fail_sum)) print("未通过前一项值:未通过后一项值:差值:") print(result) if __name__ == '__main__': read_asc("E:\\package\\107(2).asc") 这段代码报错”index 3 is out of bounds for axis 1 with size 3“,修改优化
时间: 2024-04-15 21:27:06 浏览: 102
dataanaly 2_numpy_pandas_python3数据库_python_产品经理_
这个错误是因为你尝试访问一个超出数组索引范围的列。根据你的代码,`usecols`列表中只包含了0、3、4三个列的索引,但是`file`数组中的列索引是从0开始的,所以你需要将`usecols`中的索引值减去1。
修改后的代码如下:
```python
def read_asc(filepath):
usecols = [1, 4, 5] # 仅读取第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)
```
这样修改后,`usecols`列表中的索引值就与实际的列索引对应了。
阅读全文