import numpy as np import pandas as pd import xlrd import os import matplotlib.pyplot as plt def get_all_fill_paths(dir_path): file_paths = [] for root, _, files in os.walk(dir_path): for file in files: file_paths.append(os.path.join(root, file)) return file_paths dir_path = r'C:\Users\lxz15\Desktop\电流数据2' file_paths = get_all_fill_paths(dir_path) data = pd.DataFrame() # 定义一个空的 DataFrame all_a = [] for i, file_path in enumerate(file_paths): df = pd.read_excel(file_path) for j in range(0, 1): for k in range(0, 1): a = pd.DataFrame(df.iloc[2 + 3 * k:5 + 3 * k, 7 + j].values) # 将列表转换为 DataFrame data = pd.concat([data, a], axis=1) # 将 a 添加到 data 中 all_a.append(a) all_a = np.array(all_a, dtype=float) print(all_a) all_a = np.where(np.char.isdigit(all_a.astype(str)), all_a, np.nan) # all_a = all_a.astype(float) all_a_avg = np.nanmean(all_a, axis=(0, 1)) print(all_a_avg) # 计算平均数 diff = all_a - all_a_avg print(diff) # 计算差 diff_sum = np.nansum(np.square(diff)) print(diff_sum) # 计算差的平方和 diff_sqrt = np.sqrt(diff_sum) # 计算平方根 print(diff_sqrt)怎么改
时间: 2024-02-13 10:05:07 浏览: 140
python 3.74 运行import numpy as np 报错lib\site-packages\numpy\__init__.py
在上面的代码中,有一个语法错误,即在第一行代码中,导入numpy和pandas库的语句之间没有加上换行符。另外,还有一些变量没有定义,比如`dir_path`,你需要定义它并赋值。如果你的代码中存在其他问题,可以在提供更多的上下文之后再进行修改。以下是修正后的代码:
```python
import numpy as np
import pandas as pd
import xlrd
import os
import matplotlib.pyplot as plt
def get_all_fill_paths(dir_path):
file_paths = []
for root, _, files in os.walk(dir_path):
for file in files:
file_paths.append(os.path.join(root, file))
return file_paths
dir_path = r'C:\Users\lxz15\Desktop\电流数据2'
file_paths = get_all_fill_paths(dir_path)
data = pd.DataFrame()
all_a = []
for i, file_path in enumerate(file_paths):
df = pd.read_excel(file_path)
for j in range(0, 1):
for k in range(0, 1):
a = pd.DataFrame(df.iloc[2 + 3 * k:5 + 3 * k, 7 + j].values)
data = pd.concat([data, a], axis=1)
all_a.append(a)
all_a = np.array(all_a, dtype=float)
print(all_a)
all_a = np.where(np.char.isdigit(all_a.astype(str)), all_a, np.nan)
all_a_avg = np.nanmean(all_a, axis=(0, 1))
print(all_a_avg)
diff = all_a - all_a_avg
print(diff)
diff_sum = np.nansum(np.square(diff))
print(diff_sum)
diff_sqrt = np.sqrt(diff_sum)
print(diff_sqrt)
```
阅读全文