AnatallDataMergew.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import glob
  3. import pandas as pd
  4. # Step 1: Define the starting path and file pattern
  5. start_path = r"C:\Users\aswen\Desktop\Code\Validation3"
  6. file_pattern = '*anat*.csv'
  7. # Step 2: Find all matching CSV files in the specified directory and its subdirectories
  8. csv_files = glob.glob(os.path.join(start_path, '*','*', file_pattern), recursive=True)
  9. # Step 3: Initialize an empty DataFrame to store the extracted data
  10. combined_df = pd.DataFrame()
  11. combined_df1 = pd.DataFrame()
  12. combined_df2 = pd.DataFrame()
  13. # Step 4: Loop through the CSV files and extract the specified columns
  14. for csv_file in csv_files:
  15. try:
  16. df = pd.read_csv(csv_file)
  17. selected_columns = ["FileAddress", "corresponding_img","Goasting", "SNR Chang", "SNR Normal"]
  18. df = df[selected_columns]
  19. df["dataset"] = csv_file.split(os.sep)[-3]
  20. # Concatenate the current DataFrame with the combined DataFrame
  21. combined_df1 = pd.concat([combined_df1, df], ignore_index=True)
  22. except Exception as e:
  23. print(f"Error reading {csv_file}: {e}")
  24. for csv_file in csv_files:
  25. try:
  26. df = pd.read_csv(csv_file)
  27. selected_columns = ["FileAddress", "img name","Goasting", "SNR Chang", "SNR Normal"]
  28. df = df[selected_columns]
  29. df["dataset"] = csv_file.split(os.sep)[-3]
  30. # Concatenate the current DataFrame with the combined DataFrame
  31. combined_df2 = pd.concat([combined_df2, df], ignore_index=True)
  32. except Exception as e:
  33. print(f"Error reading {csv_file}: {e}")
  34. combined_df2=combined_df2.rename({"img name": "corresponding_img"}, axis=1)
  35. combined_df = pd.concat([combined_df1, combined_df2], ignore_index=True)
  36. # Step 5: Print the combined DataFrame
  37. print(combined_df)
  38. # Optionally, you can save the combined DataFrame to a CSV file
  39. p = r"C:\Users\aswen\Desktop\Code\AIDAqc_Figures\input"
  40. combined_df.to_csv(os.path.join(p,'combined_data_anat.csv'), index=False)