0

I would like to convert a python dict to a pandas dataframe. The number of columns should be equal to number of values for a particular key. From the example below, column names should be i, a, b, c, and d where i is dictionary key and rest are values:

e = {}
for i in range(0,len(length_of_the_row)):
    e[i] = a,b,c,d 
0

2 Answers 2

2

Try .from_dict():

import pandas as pd
e = {}
for i in range(0, len("length_of_the_row")):
    e[i] = "a", "b", "c", "d"

df = pd.DataFrame.from_dict(e, orient='index')
Sign up to request clarification or add additional context in comments.

Comments

1

Turn the dictionary directly into a dataframe and then transpose it since the rows and columns need to be swapped:

import pandas as pd
e_df = pd.DataFrame(e) 
e_df = e_df.T

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.