I am working with a CSV file with the following format,
ST 1 2 3 4
WA 10 10 5 2
OR 0 7 3 9
CA 11 5 4 12
AZ -999 0 0 11
The first row represents # of days 1-4. I want to be able to take the data for each state, example WA, 10, 10, 5, 2 and create an array with just the numbers in that row that is sorted. If I omit the first index which is WA I can do this using.
sorted(list, key=int)
Doing so would give me a list, [2,5,10,10].
What I want to do is
- Read each line of the CSV.
- Create an array of numbers using the numerical data.
- Run some calculations using array(Percent rank)
Combine the calculated values with the correct state fields. For instance if I want to add a value of 3 to the array for WA.
b.insert(list[4]), 3)to get
[2,3,5,10,10]so I can calculate rank. (Note: I am unable to use scipy so I must calculate rank using a function which I've already figured out.)
End by writing State and rank value to new csv, something like.
ST Rank WA 30 CA 26 OR 55where Rank is the rank of the given value in the array.
I am pretty new to python so any help or pointers would be greatly appreciated. I am also limited to using basic python modules.(numpy, csv....etc)
UPDATE CODE:
with open(outputDir+"needy.csv", 'rb') as f:
first = {row[0]: sorted(row[1:], key=int) for row in list(csv.reader(f))}
for key, value in first.items():
if addn in first:
g= "yes"
print key, addn, g
#print d
else:
g= "no"
print key, addn, g
value.append(300)
value.append(22)
value = sorted(value, key=int)
print "State:", key, value
When i do this the values I append will be prpoperly added and the dict will be properly sorted, but when I define n as a value, it will not be fouund. example below.
{'WA': ['1', '1', '1', '2', '2', '2', '3', '4', '4', '4', '5', '5', '5', '5', '6', '6', '7', '7', '8', '8', '8', '8', '9', '10', '10', '10', '10', '11', '11'}
The above line is what happens if I simply print out first. If I utilize the for loop and specify addn as 11 as a global function I get.
WA 11 no
State: WA ['1', '1', '1', '2', '2', '2', '3', '4', '4', '4', '5', '5', '5', '5', '6', '6', '7', '7', '8', '8', '8', '8', '9', '10', '10', '10', '10', '11', '11',..]
Being that 11 is part of the key it should return yes etc.