Question

I am trying to iterate over nested Ordered Dictionaries in Python. I know that I can do something like this:

food = OrderedDict([('Fruits', OrderedDict([('Apple', 50), ('Banana', 100), ('Pear', 200)])), 
                    ('Vegetables', OrderedDict([('Carrot', 10), ('Broccoli', 5), ('Corn', 40)]))])
for value in food.itervalues():
    for key in value.iterkeys():
        print key

to get the key (or value) using nested for loops. However, how can I do this in one line? I am trying to save only the String keys (i.e. the fruit names) to a pandas dataframe (the numbers are not used). Here was my attempt:

food_df = pd.DataFrame({'Food': key for key in value.iterkeys() for value in food.itervalues()})

Which raises the error:

File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 397, in __init__
    mgr = self._init_dict(data, index, columns, dtype=dtype)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 528, in _init_dict
    dtype=dtype)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 5670, in _arrays_to_mgr
    index = extract_index(arrays)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 5708, in extract_index
    raise ValueError('If using all scalar values, you must must pass'
    ValueError: If using all scalar values, you must must pass an index

Any ideas? Thanks!

Was it helpful?

Solution

[item for sublist in map(lambda a: a.keys(), food.itervalues()) for item in sublist]

This returns

['Apple', 'Banana', 'Pear', 'Carrot', 'Broccoli', 'Corn']

in your case.

You can then create a DataFrame as usual.

food_pd = pd.DataFrame([item for sublist in map(lambda a: a.keys(), food.itervalues()) for item in sublist], columns=["Food"])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top