Store Complex Dictionary In Pandas Dataframe
This question follows my previous one.it's a mother dictionary of the one before store dictionary in pandas dataframe I have a dictionary dictionary_example={'New York':{1234:{'
Solution 1:
The previously provided solution, as you quote, is not a very neat one. This one is more readable and provides the solution for your current problem. If possible you should reconsider your data structure though...
df = pd.DataFrame()
question_ids = [0,1,2]
Create a dataframe with a row for every city-choice combination, with dictionary in choice set column
for _, city_value in dictionary_example.iteritems():
city_df = pd.DataFrame.from_dict(city_value).T
city_df = city_df.join(pd.DataFrame(city_df["choice_set"].to_dict()).T)
df = df.append(city_df)
Join the weird column names from choice set to your df
for i in question_ids:
choice_df = pd.DataFrame(df[i].to_dict()).T
choice_df.columns = map(lambda x: "{}_{}".format(x,i), choice_df.columns)
df = df.join(choice_df)
Fix the city columns
df = pd.get_dummies(df, prefix="", prefix_sep="", columns=['city'])
df.drop(question_ids + ['choice_set'], axis=1, inplace=True)
# Optional to remove NaN from questions:
# df = df.fillna(0)
df
Post a Comment for "Store Complex Dictionary In Pandas Dataframe"