-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrearrangefiles
More file actions
executable file
·61 lines (53 loc) · 2.3 KB
/
rearrangefiles
File metadata and controls
executable file
·61 lines (53 loc) · 2.3 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
# -*- coding: latin-1 -*-
#
# Copyright 2019-2025 Blaise Frederick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
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")