Home > Net >  create dataframe from nested dictionary
create dataframe from nested dictionary

Time:12-09

I have a dictionary as given below:-

d = {'Tom'  :{'A' : '25', 'B' : '34'}, 
     'Jim'  : {'A' : '34', 'B' : '19'}, 
     'john' : {'A' :'56' , 'B' : '32'}}

How can I construct the dataframe like this from the above dictionary?

Names A B
Tom 25 34
Jim 34 19
John 56 32

CodePudding user response:

Use the pandas.DataFrame.from_dict specifying the orientation of index, eg:

df = pd.DataFrame.from_dict(d, orient='index')

(Then optionally reset your index and give it a name..., eg:)

df = pd.DataFrame.from_dict(d, orient='index').rename_axis('Names').reset_index()

CodePudding user response:

Just create a DataFrame with d and transpose it:

import pandas as pd

pd.DataFrame(d).transpose()

OUTPUT

       A   B
Tom   25  34
Jim   34  19
john  56  32
  • Related