python - store dictionary in pandas dataframe -


i want store a dictionary data frame

dictionary_example={1234:{'choice':0,'choice_set':{0:{'a':100,'b':200,'c':300},1:{'a':200,'b':300,'c':300},2:{'a':500,'b':300,'c':300}}},    234:{'choice':1,'choice_set':0:{'a':100,'b':400},1:{'a':100,'b':300,'c':1000}},    1876:{'choice':2,'choice_set':0:{'a': 100,'b':400,'c':300},1:{'a':100,'b':300,'c':1000},2:{'a':600,'b':200,'c':100}}   } 

that put them

id choice  0_a  0_b  0_c  1_a  1_b  1_c  2_a  2_b  2_c   1234  0     100  200 300  200  300  300  500  300  300 234  1      100  400  -   100  300  1000  -    -    - 1876  2     100  400  300  100  300  1000 600 200 100 

i think following pretty close, core idea convert dictionaries json , relying on pandas.read_json parse them.

dictionary_example={         "1234":{'choice':0,'choice_set':{0:{'a':100,'b':200,'c':300},1:{'a':200,'b':300,'c':300},2:{'a':500,'b':300,'c':300}}},        "234":{'choice':1,'choice_set':{0:{'a':100,'b':400},1:{'a':100,'b':300,'c':1000}}},        "1876":{'choice':2,'choice_set':{0:{'a': 100,'b':400,'c':300},1:{'a':100,'b':300,'c':1000},2:{'a':600,'b':200,'c':100}}}      }  df = pd.read_json(json.dumps(dictionary_example)).t   def to_s(r):     return pd.read_json(json.dumps(r)).unstack()  flattened_choice_set = df["choice_set"].apply(to_s)  flattened_choice_set.columns = ['_'.join((str(col[0]), col[1])) col in flattened_choice_set.columns]   result = pd.merge(df, flattened_choice_set,           left_index=true, right_index=true).drop("choice_set", axis=1)  result 

enter image description here


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -