dictionary - Intersections between values in two dictionaries in python -
i have csv file contains trade data countries. data has format follows:
rep par commodity value usa ger 1 700 usa ger 2 100 usa ger 3 400 usa ger 5 100 usa ger 80 900 ger usa 2 300 ger usa 4 500 ger usa 5 700 ger usa 97 450 ger uk 50 300 uk usa 4 1100 uk usa 80 200 uk ger 50 200 uk ger 39 650 i intend make new dictionary , using created dictionary, calculate total value of common traded commodities between countries. example, consider trade between usa-ger, intend check whether ger-usa in data , if exists, values common commodities summed , same countries. dictionary should like:
dic_c1c2_producs= {('usa','ger'): ('1','700'),('2','100'),('3','400'),('5','100'),('80','900'); ('ger','usa'):('2','300'),('4','500'),('5','700'),('97','450') ; ('ger','uk'):('50','300'); ('uk','usa'): ('4','80'),('80','200'); ('uk','ger'): ('50','200'),('39','650')} as can see, usa-ger , ger-usa have commodities 2 , 5 in common , value of these goods (100+300)+(100+700). pairs usa-uk , uk-usa, have common commodities: 0 total trade 0 well. ger-uk , uk-ger, commodity 50 in common , total trade 300+200. @ end, want have like:
dic_c1c2_summation={('usa','ger'):1200;('ger','uk'):500; ('uk','usa'):0} any appreciated.
in addition post, have written following lines:
from collections import defaultdict rfile = csv.reader(open("filepath",'r')) rfile.next() dic_c1c2_products = defaultdict(set) dic_c_products = {} country = set() row in rfile : c1 = row[0] c2 = row[1] p = row[2] country.add(c1) in country : dic_c_products[i] = set() rfile = csv.reader(open("filepath")) rfile.next() in rfile: c1 = i[0] c2 = i[1] p = i[2] v=i[3] dic_c_products[c1].add((p,v)) if not dic_c1c2_products.has_key((c1,c2)) : dic_c1c2_products[(c1,c2)] = set() dic_c1c2_products[(c1,c2)].add((p,v)) else: dic_c1c2_products[(c1,c2)].add((p,v)) c_list = dic_c_products.keys() dic_c1c2_productsummation = set() in dic_c1c2_products.keys(): if dic_c1c2_products.has_key((i[1],i[0])): p1, v1 in dic_c1c2_products[(i[0],i[1])]: p2, v2 in dic_c1c2_products[(i[1],i[0])]: if p1==p2: summation=v1+v2 if not in dic_c1c2_productsum.keys(): dic_c1c2_productsum[(i[0],i[1])]=(p1, summation) else: dic_c1c2_productsum[(i[0],i[1])].add((p1, summation)) else: dic_c1c2_productsn[i] = " "
# save data in file called data import pandas pd data = pd.read_csv('data', delim_whitespace=true) data['par_rep'] = data.apply(lambda x: '_'.join(sorted([x['par'], x['rep']])), axis=1) result = data.groupby(('par_rep', 'commodity')).filter(lambda x: len(x) >= 2).groupby(('par_rep'))['value'].sum().to_dict() at end result {'ger_uk': 500, 'ger_usa': 1200}
Comments
Post a Comment