-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrearrangefiles
executable file
·44 lines (36 loc) · 1.68 KB
/
rearrangefiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
import sys # this goes up at the top where you are importing things
import numpy as np
import pandas as pd
if (
len(sys.argv) != 2
): # sys.argv[0] is always the name of the program, so the first command line argument is sys.argv[1]. Because arrays start at 0, that means the length is 2 if you have one argument.
print("you should have one and only argument")
sys.exit()
thefilenames = sys.argv[1]
# read the filenames in from the file with pandas, convert the 0th column to a python list
nameframe = pd.read_csv(thefilenames, header=None, engine="python")
thefiles = nameframe.ix[:, 0].tolist()
output_df = None
for thefilename in thefiles:
df = pd.read_csv(thefilename, engine="python")
columnlist = df.columns.values.tolist()[
1:
] # the index at the end cuts out the first column, which is the row names, not data
rowlist = df["sources"].values.tolist()
names = []
values = []
for row in range(len(rowlist)):
for col in range(len(columnlist)):
names.append(rowlist[row] + " " + columnlist[col])
values.append(df[columnlist[col]][row])
thedata = {thefilename: np.asarray(values, dtype=np.float64)}
if output_df is None:
print(thefilename, "is the first file - initializing output frame")
output_df = pd.DataFrame(data=thedata, index=np.asarray(names, dtype=np.str))
else:
print(thefilename, "is a subsequent file - appending to output frame")
temp_df = pd.DataFrame(data=thedata, index=np.asarray(names, dtype=np.str))
output_df = pd.merge(output_df, temp_df, right_index=True, left_index=True)
print(output_df)
output_df.to_csv("allsubjects.csv")