'I am trying to sum candidate votes by summing up votes for all counties in a state for a candidate
Here is my code. It only returns 0 for all keys. I want to know why my loop is not summing votes for the same state and candidate.
with open(IN_PATH, 'r') as in_file:
dict_reader = csv.DictReader(in_file)
tuple_dict = {(i["year"], i["state_po"], i["county_name"], i["candidate"]): int(i["candidatevotes"])
if i["candidatevotes"] != "NA" else i["candidatevotes"] == "" for i in dict_reader if i["year"] == "2020"}
count_dict = {(k[0], k[1], k[3]):0 for k in tuple_dict.keys()}
for k,v in tuple_dict.items():
if k[1] and k[3] in count_dict:
count_dict[k[1]] += v
else:
count_dict[k[1]] = v
Here is a sample of my output:
{('2020', 'AL', 'JOSEPH R BIDEN JR'): 0, ('2020', 'AL', 'OTHER'): 0, ('2020', 'AL', 'DONALD J TRUMP'): 0, ('2020', 'AK', 'JOSEPH R BIDEN JR'): 0, ('2020', 'AK', 'OTHER'): 0, ('2020', 'AK', 'JO JORGENSEN'): 0, ('2020', 'AK', 'DONALD J TRUMP'): 0, ('2020', 'AZ', 'JOSEPH R BIDEN JR'): 0, ('2020', 'AZ', 'OTHER'): 0, ('2020', 'AZ', 'JO JORGENSEN'): 0, ('2020', 'AZ', 'DONALD J TRUMP'): 0, ('2020', 'AR', 'JOSEPH R BIDEN JR'): 0, ('2020', 'AR', 'OTHER'): 0, ('2020', 'AR', 'JO JORGENSEN'): 0, ('2020', 'AR', 'DONALD J TRUMP'): 0, ('2020', 'CA', 'JOSEPH R BIDEN JR'): 0, ('2020', 'CA', 'OTHER'): 0, ('2020', 'CA', 'JO JORGENSEN'): 0, ('2020', 'CA', 'DONALD J TRUMP'): 0, ('2020', 'CO', 'JOSEPH R BIDEN JR'): 0, ('2020', 'CO', 'OTHER'): 0, ('2020', 'CO', 'JO JORGENSEN'): 0, ('2020', 'CO', 'DONALD J TRUMP'): 0, ('2020', 'CT', 'JOSEPH R BIDEN JR'): 0, ('2020', 'CT', 'OTHER'): 0, ('2020', 'CT', 'JO JORGENSEN'): 0, ('2020', 'CT', 'DONALD J TR
Here is a sample of my data:
"year","state","state_po","county_name","county_fips","office","candidate","party","candidatevotes","totalvotes","version","mode"
2000,"ALABAMA","AL","AUTAUGA","1001","PRESIDENT","AL GORE","DEMOCRAT",4942,17208,20191203,"TOTAL"
2000,"ALABAMA","AL","AUTAUGA","1001","PRESIDENT","GEORGE W. BUSH","REPUBLICAN",11993,17208,20191203,"TOTAL"
2000,"ALABAMA","AL","AUTAUGA","1001","PRESIDENT","RALPH NADER","GREEN",160,17208,20191203,"TOTAL"
2000,"ALABAMA","AL","AUTAUGA","1001","PRESIDENT","OTHER","OTHER",113,17208,20191203,"TOTAL"
2000,"ALABAMA","AL","BALDWIN","1003","PRESIDENT","AL GORE","DEMOCRAT",13997,56480,20191203,"TOTAL"
Solution 1:[1]
for k,v in tuple_dict.items():
if k[1] and k[3] in count_dict:
count_dict[k[1]] += v
else:
count_dict[k[1]] = v
you are assigning a tuple as the key when you are creating the count_dict, and then trying to access the dictionary using a single value as a key. this is creating a new key and then storing values there instead of adding it to the key you intended to add it to.
this is what you actually need to do, as far as i understand.
for k, v in tuple_dict.items():
if k[1] in count_dict and k[3] in count_dict: # check k[1] and k[3] are in the count dict
count_dict[(k[0], k[1], k[3])] += v
else:
count_dict[(k[0], k[1], k[3])] = v
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rohit Patil |
