Search code examples
ordereddict

How to create dataframe from ordered dictionary?


I have an ordered dictionary which has 4 keys and multiple values. I tried to create the dataframe like this

  df = pd.DataFrame(items, index=[0])
  print('\ndf is ',df)

But this triggers ValueError, as the multiple values from the dictionary don't match. The ordered dictionary is below:

OrderedDict([('Product', 'DASXZSDASXZS'), ('Region', ['A', 'B', 'C']), ('Items', ['1', '2', '3']), ('Order', ['123', '456', '789'])])

I want the dataframe format to be like:

Product      Region Items Order
DASXZSDASXZS A      1     123
DASXZSDASXZS B      2     456
              ...

How can I achieve this format for the dataframe?


Solution

  • Not enough rep to comment. Why do you try to specify index=[0]? Simply doing

    df = pd.DataFrame(items)
    

    works; if you want to change the index, you can set it later with df.set_index(...)