0

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

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

It gives the dataframe with another index as 0,1,2. Is there a way we can remove that and just get the columns Name, A and B ?
Umm... don't reset the index then... it makes more sense to have your name as an index anyway... (if you want it named.... then remove the .reset_index() from the 2nd example)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.