python - pandas to multiple dict objects -
i have scoring scale in table/dataframe. want read read frame , convert multiple dict objects. have table:
col_ref col_cutoff col_value c1 10 100 c1 20 200 c1 miss 500 c1 null 100 c2 250 c2 b 200 c2 null 0 c2 miss 100
i want convert 2 dict objects:
c1_dict = { 'miss' : 500, 'null' :100, 'vals' : [ (10, 100), (20, 2000) ] } c2_dict = { 'miss' : 100, 'null' :0, 'vals' : [ (a, 250), (b, 200) ] }
here started with.. can't figure further
import pandas pd def panda_2_dict(pd): pdf_ref = pd.read_csv() col_refs = pd.pdf_ref.[col_ref.distinct] each in col_refs col_ref_i = {col_cutoff:col_val} return col_ref_i
i expecting list of dict objects(here 2).
you can try:
col_ref col_cutoff col_value 0 c1 10 100 1 c1 20 200 2 c1 miss 500 3 c1 null 100 4 c2 250 5 c2 b 200 6 c2 null 0 7 c2 miss 100 gb = df.groupby('col_ref') k, v in gb: print k = (v[:2].set_index('col_cutoff')['col_value'].to_dict()).items() b = v[2:].set_index('col_cutoff')['col_value'].to_dict() b['vals'] = print b c1 {'vals': [('10', 100), ('20', 200)], 'null': 100, 'miss': 500} c2 {'vals': [('a', 250), ('b', 200)], 'null': 0, 'miss': 100}
Comments
Post a Comment