How to Convert Asymmetrical Dictionary to a pandas DataFrame

Viewed 114
dict_circles = {
    'radii': [1, 5, 10, 15], 
    'feature_group': [1, 5, 10, 15], 
    'lat_long': {'city1': [lat1, long1], 
                 'city2': [lat2, long2]
                }
}

I'm working with the dictionary above and would like to create the following pandas DataFrame by splitting off the lat & long values and then duplicating them to create symmetry in a new DF:

          **radii** **feature_group** **lat** **long**
    city1     1            1          lat1   long1
    city2     1            1          lat2   long2
    city1     5            5          lat1   long1
    city2     5            5          lat2   long2
    city1    10           10          lat1   long1
    city2    10           10          lat2   long2
    city1    15           15          lat1   long1
    city2    15           15          lat2   long2

From what I can tell I will need a recursion [isinstance(data, type) or other..] function to access the list nested within the inner dictionary 'lat_long', and probably also to use 'pd.DataFrame.from_dict()', and maybe a dictionary comprehension. The solution escapes me. If there's a better strategy please advise.

1 Answers

Here is one way to work with dict_circles:

import pandas as pd
# I multiplied each feature group by 10, to distinguish vs radii
dict_circles = {
    'radii': [1, 5, 10, 15], 
    'feature_group': [10, 50, 100, 150], 
    'lat_long': {'city1': ['lat1', 'long1'], 
                 'city2': ['lat2', 'long2']
                }
}

# convert dict_circles (which is a nested dict) to list-of-tuples
tuples = [(city, r, fg, lat, lon)
          for r, fg in zip(dict_circles['radii'], dict_circles['feature_group'])
          for city, (lat,lon) in dict_circles['lat_long'].items()
         ]

# the list-of-tuples is compatible with the DataFrame constructor
df = pd.DataFrame(tuples, 
                  columns=('city', 'radii', 'feature_group', 'lat', 'long'))
print(df)

    city  radii  feature_group   lat   long
0  city1      1             10  lat1  long1
1  city2      1             10  lat2  long2
2  city1      5             50  lat1  long1
3  city2      5             50  lat2  long2
4  city1     10            100  lat1  long1
5  city2     10            100  lat2  long2
6  city1     15            150  lat1  long1
7  city2     15            150  lat2  long2
Related