Python: How to generate DataFrame from 2 keys exist in the big dictionary

Viewed 44

I am currently working with dictionary D that looks like this:

D = {
     'Instagram': [{'total_post': 3}], 
     'Twitter': [{'total_post': 9}], 
     'Facebook': [{'total_post': 1}], 
     'YouTube': [{'total_post': 5}]
    }

I want to make a DataFrame with this structure:

          total_post
Instagram 3
Twitter   9
Facebook  1
YouTube   5

I think I need to take the key (Instagram, Twitter, Facebook, YouTube, total_post) to make the desired DataFrame, but currently I am having problem finding how to do it. This is my current code, that returns final_df:

list_dataframe = [] #populate dictionary values to a list
for key, values in D.items():
   df = pd.DataFrame(values)
   list_dataframe.append(df)

# combine dataframe
indexed_df = [df.set_index(index) for df in list_dataframe] #my current index = total_post
final_df = pd.concat(indexed_df, axis=1) 
final_df.fillna(0, inplace=True)

This is the list_dataframe current result:

[   total_post
0           3,    total_post
0           9,    total_post
0           1,    total_post
0           5]

What can I do to take keys? Or, is there any better way to process the dictionary D?

1 Answers

Use list comprehension for select first value of nested lists and key total_post:

df = pd.DataFrame([v[0]['total_post'] for k, v in D.items()], 
                  index=D.keys(), 
                  columns=['total_post'])
print (df)
           total_post
Instagram           3
Twitter             9
Facebook            1
YouTube             5

Or use DataFrame.from_dict:

df =  pd.DataFrame.from_dict({k:v[0] for k, v in D.items()}, orient='index')
print (df)
           total_post
Facebook            1
Instagram           3
Twitter             9
YouTube             5
Related