Allenfenqu/caculate_mean.py

38 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import pandas as pd
import numpy as np
# 设置包含文件夹的主文件夹路径
main_folder = 'D:/桌面/fenqu_liziqun/output'
# 创建一个空的DataFrame来存储所有文件的数据
all_data = None
# 遍历主文件夹中的所有子文件夹
for folder_name in os.listdir(main_folder):
folder_path = os.path.join(main_folder, folder_name)
# 检查文件夹是否存在
if os.path.isdir(folder_path):
# 构建Excel文件的完整路径
excel_file_path = os.path.join(folder_path, 'lixiangjie_and_best_chromosome.xlsx')
# 检查Excel文件是否存在
if os.path.exists(excel_file_path):
# 读取Excel文件
df = pd.read_excel(excel_file_path)
# 如果all_data为空直接将df复制给all_data
if all_data is None:
all_data = df
else:
# 否则将df中的值逐个加到all_data中
all_data += df
# 计算所有文件中每个位置上的平均值
average_data = all_data / len(os.listdir(main_folder))
average_data.to_excel('D:/桌面/分方向MFD实验/13理想解可视化和最好的染色体/lixiangjie_and_best_chromosome4.xlsx', index=False)
# 打印最终结果
print(average_data)