I'm trying to combine CSV files in a folder to analyze them. Also, I want to append the filename of each as a column so I can figure out which data came from which file. I've looked at the similar questions and none of them have worked for me.
Here's the code I'm using. There are 24 CSV files in this folder and since combining CSV files later would be easy using cat so even a method to tell me how I could append the filename in each file would be perfect. Any help would be great.
import pandas as pd
import os
import glob
import csv
path=r'/home/videept/Downloads/A_DeviceMotion_data/A_DeviceMotion_data/dws_1/'
with open('output.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
for filename in glob.glob(os.path.join(path,"*.csv")):
with open(filename, newline='') as f_input:
csv_input = csv.reader(f_input)
for row in csv_input:
row.insert(0, filename)
csv_output.writerow(row)
When I'm doing this the cell goes on an infinite loop and no new file is even created. I'm not sure how I can see the progress of what's going on so any idea on that would also be great. Thanks :)
print("Processing", filename, "...)just before thewith open(filename, newline='')...line to be sure whether one file is blocking everything. If not enough, I would also add a trace every n rows with something like:for i,row in enumerate(csv_input): if (0 == i%n): print('.', end='') ...print()to see what you have in variables - ie.filenameandrow