数据集合并与处理

Created on: September 28, 2024

Answered using OpenAI 01 Mini by OpenAI01

Question

# First, let's identify the different data sets from the files you provided.
# We have several names such as dolphin, golden_fish, griffen, mermaid, seahorse, shark, butterfly, unicorn.
# We will group them accordingly and then combine the same datasets across different algorithms.

import os



# Prepare the base names for grouping
base_names = ["butterfly", "dolphin", "golden_fish", "griffen", "mermaid", "seahorse", "shark", "unicorn"]


# Function to load the CSV files and combine them for comparison by dataset
def load_and_combine(base_name):
# Load the CSV files for the specified base name
ppo_df = pd.read_csv(f"/mnt/data/{base_name}_ppo.csv")
sac_df = pd.read_csv(f"/mnt/data/{base_name}_sac.csv")
xu_df = pd.read_csv(f"/mnt/data/{base_name}_xu.csv")
zhao_df = pd.read_csv(f"/mnt/data/{base_name}_zhao.csv")

# Combine into a single dataframe
combined_df = pd.DataFrame(
{
"指标": ppo_df["指标"],
f"{base_name}_ppo": ppo_df["数值"],
f"{base_name}_sac": sac_df["数值"],
f"{base_name}_xu": xu_df["数值"],
f"{base_name}_zhao": zhao_df["数值"],
}
)

return combined_df


# Initialize a dictionary to store combined dataframes for each dataset
combined_datasets = {base: load_and_combine(base) for base in base_names}

# Save each combined dataset into CSV
for base, df in combined_datasets.items():
df.to_csv(f"/mnt/data/comparison_{base}.csv", index=False)

# List all the generated CSV files for the user to download
generated_files = [f"comparison_{base}.csv" for base in base_names]
generated_files

对于以上代码,参考下面文件进行修改
❯ ls
3_ppo.csv 5_sac.csv 7_xu.csv butterfly_zhao.csv dolphin_zhao.csv griffen_ppo.csv mermaid_sac.csv seahorse_xu.csv shark_zhao.csv
3_sac.csv 5_xu.csv 7_zhao.csv data_gen.py golden_fish_ppo.csv griffen_sac.csv mermaid_xu.csv seahorse_zhao.csv unicorn_ppo.csv
3_xu.csv 5_zhao.csv butterfly_ppo.csv dolphin_ppo.csv golden_fish_sac.csv griffen_xu.csv mermaid_zhao.csv shark_ppo.csv unicorn_sac.csv
3_zhao.csv 7_ppo.csv butterfly_sac.csv dolphin_sac.csv golden_fish_xu.csv griffen_zhao.csv seahorse_ppo.csv shark_sac.csv unicorn_xu.csv
5_ppo.csv 7_sac.csv butterfly_xu.csv dolphin_xu.csv golden_fish_zhao.csv mermaid_ppo.csv seahorse_sac.csv shark_xu.csv unicorn_zhao.csv

Thought

加载并合并CSV文件

我正在查看用户加载的四个CSV文件,合并并保存到一个单一的DataFrame中。_files显示了各个CSV文件的名称。

检查数据集

我正在检查.ls输出中包含的文件,发现还有其他数据集“3”、“5”、“7”,需要修改代码以适应这些数据集。代码原本定义base_names为动物名称,现在需要包括数字数据集。

针对数据集的处理方法

Answer

好的,根据您提供的文件列表,除了现有的动物名称(如 butterfly, dolphin, 等),还有数字名称的数据集(如 3, 5, 7)。为了处理所有这些数据集,您需要将这些数字名称也包含在 base_names 列表中。以下是修改后的代码:

import os import pandas as pd # 更新后的基准名称列表,包含数字和动物名称 base_names = ["3", "5", "7", "butterfly", "dolphin", "golden_fish", "griffen", "mermaid", "seahorse", "shark", "unicorn"] # 定义需要使用的算法后缀 algorithms = ["ppo", "sac", "xu", "zhao"] # 检查所有需要的文件是否存在 def check_files_exist(base_name, algorithms, directory="/mnt/data/"): missing_files = [] for algo in algorithms: file_path = os.path.join(directory, f"{base_name}_{algo}.csv") if not os.path.isfile(file_path): missing_files.append(file_path) return missing_files # 函数:加载并合并指定基准名称的CSV文件 def load_and_combine(base_name, algorithms, directory="/mnt/data/"): data_frames = {} # 加载每个算法对应的CSV文件 for algo in algorithms: file_path = os.path.join(directory, f"{base_name}_{algo}.csv") try: df = pd.read_csv(file_path) data_frames[algo] = df except FileNotFoundError: print(f"文件未找到: {file_path}") return None # 假设所有CSV文件都有相同的“指标”列 combined_df = pd.DataFrame() combined_df["指标"] = data_frames[algorithms[0]]["指标"] # 将每个算法的“数值”列添加到合并的DataFrame中 for algo in algorithms: combined_df[f"{base_name}_{algo}"] = data_frames[algo]["数值"] return combined_df # 初始化一个字典来存储每个数据集的合并后的DataFrame combined_datasets = {} # 检查并加载所有基准名称的数据集 for base in base_names: missing = check_files_exist(base, algorithms) if missing: print(f"以下文件缺失,跳过 '{base}' 数据集:") for file in missing: print(f" - {file}") continue combined_df = load_and_combine(base, algorithms) if combined_df is not None: combined_datasets[base] = combined_df # 将每个合并后的数据集保存为CSV文件 for base, df in combined_datasets.items(): output_path = os.path.join("/mnt/data/", f"comparison_{base}.csv") df.to_csv(output_path, index=False) print(f"已保存: {output_path}") # 列出所有生成的CSV文件供用户下载 generated_files = [f"comparison_{base}.csv" for base in combined_datasets.keys()] print("生成的文件列表:") for file in generated_files: print(f" - {file}")

主要修改点:

  1. 扩展 base_names 列表

    • 添加了 "3", "5", 和 "7"base_names 中,以确保这些数据集也被处理。
  2. 动态算法列表

    • 将算法后缀("ppo", "sac", "xu", "zhao")提取到一个单独的 algorithms 列表中,以便于管理和扩展。
  3. 文件存在性检查

    • 添加了 check_files_exist 函数,用于在尝试加载文件之前检查所有必要的CSV文件是否存在。这有助于避免因缺失文件导致的错误,并向用户提示缺失的文件。
  4. 增强的错误处理

    • load_and_combine 函数中,添加了异常处理以捕捉 FileNotFoundError,并在缺少文件时跳过该数据集。
  5. 输出和提示

    • 添加了打印语句,以便用户了解哪些文件被保存以及哪些数据集由于缺失文件而被跳过。

运行结果:

运行上述代码后,您将得到所有存在的 comparison_<base_name>.csv 文件,这些文件将包含每个数据集在不同算法下的对比数据。缺失任何必要的CSV文件的数据集将被跳过,并在控制台中显示缺失的文件路径。

示例输出:

已保存: /mnt/data/comparison_3.csv
已保存: /mnt/data/comparison_5.csv
已保存: /mnt/data/comparison_7.csv
已保存: /mnt/data/comparison_butterfly.csv
已保存: /mnt/data/comparison_dolphin.csv
...
生成的文件列表:
 - comparison_3.csv
 - comparison_5.csv
 - comparison_7.csv
 - comparison_butterfly.csv
 - comparison_dolphin.csv
 - ...

这样,所有指定的数据集都将被处理,并生成相应的对比CSV文件,方便您进一步分析和下载。

Share this Q&A