Wakefield's Daily Tips

Merging data files

Here is a short PsychoPy Coder script which will merge the CSV files in a folder into a single csv file in the parent folder.

# Created using ChatGPT
import os
import pandas as pd
from tkinter import Tk
from tkinter.filedialog import askdirectory

def merge_csv_files(output_file):
    # Create a Tk root window (it will be hidden)
    Tk().withdraw()
    
    # Ask the user to select a folder containing the CSV files
    input_folder = askdirectory(title="Select Folder Containing CSV Files")
    
    if not input_folder:
        print("No folder selected. Exiting...")
        return
    
    # List all CSV files in the input folder
    csv_files = [f for f in os.listdir(input_folder) if f.endswith('.csv')]
    
    if not csv_files:
        print("No CSV files found in the selected folder.")
        return
    
    # Initialize an empty list to hold DataFrames
    dfs = []
    
    for file in csv_files:
        # Construct full file path
        file_path = os.path.join(input_folder, file)
        
        # Read each CSV file into a DataFrame
        df = pd.read_csv(file_path, encoding='windows-1256')
        
        # Append the DataFrame to the list
        dfs.append(df)
    
    # Concatenate all DataFrames vertically (stacking them)
    merged_df = pd.concat(dfs, ignore_index=True)
    
    # Write the merged DataFrame to a new CSV file in folder above data folder
    merged_df.to_csv(input_folder + "/../" + output_file, index=False, encoding='windows-1256')
    print(f'Merged CSV saved as: {output_file}')

# Usage
output_file = 'merged_files.csv'  # Set the output file name
merge_csv_files(output_file)

merge_csv_files.py (1.5 KB)