1

My dictionary looks like this

mydict = 
{240594.0: {1322.0: 1.6899999999999999, 1323.0: 1.6900000000000002, 1324.0: 1.6899999999999999, 1325.0: 1.6899999999999999, 1326.0: 1.6899999999999999, 1327.0: 1.6900000000000002, 1328.0: 1.6899999999999999, 1329.0: 1.6899999999999999, 1356.0: 1.6900000000000002, 1357.0: 1.6900000000000002, 1358.0: 1.6899999999999999, 1359.0: 1.6900000000000002, 1360.0: 1.6900000000000002, ...},

226918.0: {1322.0: 1.6900000000000002, 1323.0: 1.6899999999999999, 1324.0: 1.6900000000000002, 1325.0: 1.6899999999999999, 1326.0: 1.6900000000000002, 1327.0: 1.6899999999999999, 1328.0: 1.6900000000000002, 1329.0: 1.6899999999999999, 1352.0: 1.6900000000000002, 1353.0: 1.6900000000000002, 1354.0: 1.6899999999999999 ...}}

which is the real value of {iri_key: {week:price, week:price ...}, iri_key: {...}} and I want to convert this dictionary into dataframe which looks like

         week week  week ...
irikey: price price price ...
irikey: ...    ...   ...

in above case

           1322.0                  ...
240594.0   1.6899999999999999      ...
226918.0   1.6900000000000002      ...

how could I do this?

5
  • Do you know the range of week? Commented Mar 31, 2014 at 1:53
  • yes from 1322 to 1373 Commented Mar 31, 2014 at 2:09
  • but some of them has missing values so dimension doesn't match. still max of week is 1373 and min is 1322 Commented Mar 31, 2014 at 2:11
  • you could complete the dict by replace missing values to some invalid value like -1 Commented Mar 31, 2014 at 2:23
  • Actually pandas can take incomplete dict to construct dataframe, I think Commented Mar 31, 2014 at 2:25

1 Answer 1

2

As you have probably discovered, DataFrame(mydict) is valid code. You could simply take the transpose (.T) to get your desired result.

A better way, in terms of code readability and directness, is available: use the specific DataFrame constructor DataFrame.from_dict, which has a keyword argument orient.

In [2]: DataFrame.from_dict(mydict, orient='index')
Out[2]: 
        1356  1357  1358  1359  1360  1322  1323  1324  1325  1326  1327  \
226918   NaN   NaN   NaN   NaN   NaN  1.69  1.69  1.69  1.69  1.69  1.69   
240594  1.69  1.69  1.69  1.69  1.69  1.69  1.69  1.69  1.69  1.69  1.69   

        1328  1329  1352  1353  1354  
226918  1.69  1.69  1.69  1.69  1.69  
240594  1.69  1.69   NaN   NaN   NaN  

[2 rows x 16 columns]

As you can see from the example data you provided, missing values and variable lengths are handled properly.

Sign up to request clarification or add additional context in comments.

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.