transpose_csv.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Mar 18 22:12:25 2024
  4. @author: arefk
  5. """
  6. import pandas as pd
  7. import os
  8. # Get the directory where the code file is located
  9. code_dir = os.path.dirname(os.path.abspath(__file__))
  10. # Get the parent directory of the code directory
  11. parent_dir = os.path.dirname(code_dir)
  12. # Get the parent directory of the code directory
  13. parent_dir = os.path.dirname(code_dir)
  14. # Read the behavior data CSV file into a Pandas DataFrame
  15. input_file_path_data = os.path.join(parent_dir, 'output',
  16. "Final_Quantitative_output",
  17. 'Quantitative_results_from_dwi_processing.csv')
  18. # Read the CSV file
  19. df0 = pd.read_csv(input_file_path_data)
  20. #filter anything?
  21. df = df0[(df0["Qtype"] == "rd") &
  22. (df0["mask_name"] == "CC_MOp(dilated)_cut.tt") &
  23. (df0["dialation_amount"] == 0)]
  24. Values = ["Value"]
  25. #%%
  26. # Create an empty DataFrame to store the transposed data
  27. transposed_data = pd.DataFrame()
  28. transpodeCloulmn = "Group"
  29. for vv in Values:
  30. # Create an empty DataFrame to store the transposed data for this value
  31. value_transposed_data = pd.DataFrame()
  32. for group_value in df[transpodeCloulmn].unique():
  33. # Filter DataFrame for current group value
  34. filtered_df = df[df[transpodeCloulmn] == group_value]
  35. # Pivot the DataFrame for current value and group
  36. pivot_df = pd.pivot_table(filtered_df, values=vv, index="merged_timepoint", columns=["subjectID", transpodeCloulmn])
  37. # Concatenate pivot_df to value_transposed_data
  38. value_transposed_data = pd.concat([value_transposed_data, pivot_df], axis=1)
  39. # Rename the index in value_transposed_data
  40. value_transposed_data.index.name = vv
  41. # Concatenate value_transposed_data with transposed_data along the first dimension
  42. transposed_data = pd.concat([transposed_data, value_transposed_data, pd.DataFrame(index=[vv])])
  43. # Define the output file path
  44. output_file_path = os.path.join(parent_dir, 'output', "Final_Quantitative_output",'Transposed4Prism_FA_CC_MOP_dialated.csv')
  45. transposed_data.to_csv(output_file_path, index=True, header=True)
  46. print("Data restructured and saved successfully.")