caasswa
caasswa

Reputation: 521

Dictionary values to Dataframe?

I have a dictionary, where the key is a unique integer and the value is a dataframe row.

I want to take all the values (rows) from my dictionary and turn it into an actual dataframe. The structure of one row looks like this.

{1: {'imuid': '1192',
     'usid': 63,
     'usme': 'de'}, 
 2: {etc etc}
}

I can't quite work out which pandas function to use. I've tried from_records but that produces the error TypeError: object of type 'builtin_function_or_method' has no len().

I don't care about the iteration ID, just need the values in the dictionary to be a row. So the dataframe will have multiple rows where the columns are imuid, usid and usme.

Using pandas and python

Upvotes: 1

Views: 835

Answers (1)

JayPeerachai
JayPeerachai

Reputation: 3842

pd.DataFrame.from_dict ?

df = pd.DataFrame.from_dict(<your_dict>, orient='index')

Example:

d = {1: {'imuid': '1192',
     'usid': 63,
     'usme': 'de'}}

# convert dict to dataframe
df = pd.DataFrame.from_dict(d, orient='index')

Output:

>       imuid   usid usme
>     1 1192    63  de

Upvotes: 1

Related Questions