- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathcsvcolumn_to_scurve.py
executable file
·58 lines (42 loc) · 1.73 KB
/
csvcolumn_to_scurve.py
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
#!/usr/bin/env python3
# This is a simple script that reads in a csv file, selects a column, and then
# forms an "s-curve" graph of that column.
importargparse
importcsv
importsys
defget_data(input_file, before_column, after_column):
defget_selected_csv_rows(input_file, before_column, after_column):
forrowincsv.DictReader(input_file):
before=float(row[before_column])
after=float(row[after_column])
ifbefore>0:
delta=after/before
else:
delta=1
yielddelta
deff(input_data):
result=list(enumerate(sorted(input_data)))
count=float(len(result) -1)
return [(x[0] /count, x[1]) forxinresult]
returnf(get_selected_csv_rows(input_file, before_column, after_column))
defmain():
p=argparse.ArgumentParser(description="""
A script that reads in a csv file, splices out selected before/after
column, and then outputs a new csv file with that data in s-curve form. An
s-curve is a graph where one sorts the output %-change and graphs the %-n
vs %-change.
NOTE: We assume that the csv has a csv header that maps to the before and
after column names passed in.
""")
p.add_argument('input_file', type=argparse.FileType('r'))
p.add_argument('before_column_name', type=str)
p.add_argument('after_column_name', type=str)
args=p.parse_args()
data=get_data(args.input_file, args.before_column_name,
args.after_column_name)
w=csv.DictWriter(sys.stdout, fieldnames=['N/total', 'New/Old'])
w.writeheader()
fordindata:
w.writerow({'N/total': d[0], 'New/Old': d[1]})
if__name__=="__main__":
main()