merge_csv_sheets.py 1.1 KB

123456789101112131415161718192021222324
  1. import pandas as pd
  2. # Define paths to the CSV files
  3. csv_path_old = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\output\Quantitative_outputs\old\Quantitative_results_from_dwi_processing.csv"
  4. csv_path_new = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\output\Quantitative_outputs\Quantitative_results_from_dwi_processing.csv"
  5. output_path = r"C:\Users\aswen\Desktop\Code\2024_Ruthe_SND\output\Quantitative_outputs\Merged_Quantitative_results_from_dwi_processing.csv"
  6. # Load both CSVs into pandas DataFrames
  7. df_old = pd.read_csv(csv_path_old)
  8. df_new = pd.read_csv(csv_path_new)
  9. # Rename the 'Value' column in the old DataFrame to 'Value_with_wm_mask'
  10. df_old = df_old.rename(columns={'Value': 'Value_with_wm_mask'})
  11. # Columns to merge on
  12. merge_columns = ['subjectID', 'timePoint', 'merged_timepoint', 'Qtype', 'mask_name', 'dialation_amount', 'Group']
  13. # Merge the DataFrames on the specified columns
  14. df_merged = pd.merge(df_old, df_new, on=merge_columns)
  15. # Save the merged DataFrame to a new CSV file
  16. df_merged.to_csv(output_path, index=False)
  17. print(f"Merged CSV saved to {output_path}")