So, I wrote small test code for calculating the averages from all the csv files in a folder.
parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()
# Get command arguments
input_files_path = args.input
output = args.output
avg = []
for infile in glob.glob(input_files_path+"*.csv"):
a = np.loadtxt(infile, delimiter=",")
mean = np.mean(a, axis=0)
avg.append((mean))
print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
but the issue now, when I am trying to save the name of the file as well in the csv file like this:
parser = argparse.ArgumentParser(description='avg data from csv to csv')
parser.add_argument('--input', required = True)
parser.add_argument('--output', required = True)
args = parser.parse_args()
# Get command arguments
input_files_path = args.input
output = args.output
avg = []
for infile in glob.glob(input_files_path+"*.csv"):
a = np.loadtxt(infile, delimiter=",")
mean = np.mean(a, axis=0)
avg.append((infile,(mean)))
print infile
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
it throws an error:
np.savetxt(output,(avg), fmt = "%.1f", delimiter=", ")
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 963, in savetxt
X = np.asarray(X)
File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 235, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: cannot set an array element with a sequence
I don't seem to understand what the issue is. Maybe it is because I am trying to append a string to a float? I check the fmt
s in numpy.. but I did not find anything interesting.
/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py:773: UserWarning: loadtxt: Empty input file: "../robalo-31.1/op.csv"
warnings.warn('loadtxt: Empty input file: "%s"' % fname)
/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py:2374: RuntimeWarning: invalid value encountered in double_scalars
return mean(axis, dtype, out)
avg.append((infile,(mean)))
anywhere in your code. Could you please clarify how that fits into this?format
portion insavetxt
-- Basically you're telling numpy to save everything as a float, but you've got strings in your data. I can imagine that could mess up thedtype
argument when numpy tries to convert the list to an ndarray. Rather than usingnp.savetxt
, could I recommend that you look into the excellentcsv
module for saving the output file?CSV
is surely an option. do you suggest any function in that?