How to create dataframe from ordered dictionary?

64 views Asked by At

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?

2

There are 2 answers

0
Battleman On BEST ANSWER

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(...)

0
Ian Ozsvald On

@viktor_dmitry your comment to @Battleman links to external data, here's a solution.

In https://www.codepile.net/pile/GY336DYN you have a list of OrderedDict entries, in the example above you just had 1 OrderedDict. Each needs to be treated as a separate DataFrame construction. From the resulting list you use concat to get a final DataFrame:

ods = [OrderedDict([('MaterialNumber', '2XV9450-1AR24'), ('ForCountry'...]), 
       OrderedDict([('MaterialNumber', ...), 
       ...]
new_df = pd.concat([pd.DataFrame(od) for od in ods])
# new_df has 4 columns and many rows

Note also that 1 of your example items is invalid, you'd need to filter this out, the rest appear to be fine:

ods[21]
OrderedDict([('MaterialNumber', '4MC9672')]) # lacks the rest of the columns!