How to change name of keys in a list of dictionaries Python

Viewed 66

I have the a df that looks like this:

route_id client_lat client_lng
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0068   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0073   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066
 BR0074   -23.7424   -46.7066

As you may see, there are many latitudes and longitudes for each route_id. What I'm trying to do is to generate a list of dictionaries. Each dictionary will store client_lat and client_lng according to each route_id. Therefore, in my final list I should have exactly n number of dictionaries where n is the amount of unique route_id. From the df above I should have 3 dictionaries in my list where its items should be something like:

{'start1': 'geo!-23.7424,-46.7066',
'start2': 'geo!-23.7424,-46.7066',
'start3': 'geo!-23.7424,-46.7066',
'start4': 'geo!-23.7424,-46.7066',
...}

The amount of items per dictionary will vary depending on the amount of client_lat and client_lng there are in each route_id.

To generate this list of dictionaries with items following the structure above I am using this code:

lista = []

for i in data.route_id.unique():
    start_points = {f"start{k + 1}": "geo!" + data.loc[k, 'client_lat'] +  "," 
                    + data.loc[k, 'client_lng'] for k in range(0, len(data)) if data.loc[k, "route_id"] == i} 
    
    lista.append(start_points)

And my list looks like this:

lista = [{'start1': 'geo!-23.7424,-46.7066',
  'start2': 'geo!-23.7424,-46.7066',
  'start3': 'geo!-23.7424,-46.7066',
  'start4': 'geo!-23.7424,-46.7066',
  'start5': 'geo!-23.7424,-46.7066',
  'start6': 'geo!-23.7424,-46.7066',
  'start7': 'geo!-23.7424,-46.7066',
  'start8': 'geo!-23.7424,-46.7066',
  'start9': 'geo!-23.7424,-46.7066',
  'start10': 'geo!-23.7424,-46.7066'},
 {'start11': 'geo!-23.7424,-46.7066',
  'start12': 'geo!-23.7424,-46.7066',
  'start13': 'geo!-23.7424,-46.7066',
  'start14': 'geo!-23.7424,-46.7066',
  'start15': 'geo!-23.7424,-46.7066',
  'start16': 'geo!-23.7424,-46.7066',
  'start17': 'geo!-23.7424,-46.7066',
  'start18': 'geo!-23.7424,-46.7066',
  'start19': 'geo!-23.7424,-46.7066',
  'start20': 'geo!-23.7424,-46.7066'},
 {'start21': 'geo!-23.7424,-46.7066',
  'start22': 'geo!-23.7424,-46.7066',
  'start23': 'geo!-23.7424,-46.7066',
  'start24': 'geo!-23.7424,-46.7066',
  'start25': 'geo!-23.7424,-46.7066',
  'start26': 'geo!-23.7424,-46.7066',
  'start27': 'geo!-23.7424,-46.7066',
  'start28': 'geo!-23.7424,-46.7066',
  'start29': 'geo!-23.7424,-46.7066'}]

It is storing each dictionary inside of my list but the keys in each dictionary should go from 1 to m where m is the amount of lat and lng per route_id. Therefore, my desired output should go something like this:

[{'start1': 'geo!-23.7424,-46.7066',
  'start2': 'geo!-23.7424,-46.7066',
  'start3': 'geo!-23.7424,-46.7066',
  'start4': 'geo!-23.7424,-46.7066',
  'start5': 'geo!-23.7424,-46.7066',
  'start6': 'geo!-23.7424,-46.7066',
  'start7': 'geo!-23.7424,-46.7066',
  'start8': 'geo!-23.7424,-46.7066',
  'start9': 'geo!-23.7424,-46.7066',
  'start10': 'geo!-23.7424,-46.7066'},
 {'start1': 'geo!-23.7424,-46.7066',
  'start2': 'geo!-23.7424,-46.7066',
  'start3': 'geo!-23.7424,-46.7066',
  'start4': 'geo!-23.7424,-46.7066',
  'start5': 'geo!-23.7424,-46.7066',
  'start6': 'geo!-23.7424,-46.7066',
  'start7': 'geo!-23.7424,-46.7066',
  'start8': 'geo!-23.7424,-46.7066',
  'start9': 'geo!-23.7424,-46.7066',
  'start10': 'geo!-23.7424,-46.7066'},
 {'start1': 'geo!-23.7424,-46.7066',
  'start2': 'geo!-23.7424,-46.7066',
  'start3': 'geo!-23.7424,-46.7066',
  'start4': 'geo!-23.7424,-46.7066',
  'start5': 'geo!-23.7424,-46.7066',
  'start6': 'geo!-23.7424,-46.7066',
  'start7': 'geo!-23.7424,-46.7066',
  'start8': 'geo!-23.7424,-46.7066',
  'start9': 'geo!-23.7424,-46.7066'}]

If anyone knows how to do that I would really appreciate your help.

1 Answers

Using enumerate is always good to create indexes. You can change where i starts using argument start=1.

This does the job:

data = []
for route in df['route_id'].unique():
    data.append({f'start_{i}': f'geo!{x["client_lat"]},{x["client_lng"]}' for i, x in enumerate(df[df['route_id'] == route].to_dict('records'), start=1)})

Printing data:

[{'start_1': 'geo!-23.7424,-46.7066',
  'start_2': 'geo!-23.7424,-46.7066',
  'start_3': 'geo!-23.7424,-46.7066',
  'start_4': 'geo!-23.7424,-46.7066',
  'start_5': 'geo!-23.7424,-46.7066',
  'start_6': 'geo!-23.7424,-46.7066',
  'start_7': 'geo!-23.7424,-46.7066',
  'start_8': 'geo!-23.7424,-46.7066',
  'start_9': 'geo!-23.7424,-46.7066',
  'start_10': 'geo!-23.7424,-46.7066'},
 {'start_1': 'geo!-23.7424,-46.7066',
  'start_2': 'geo!-23.7424,-46.7066',
  'start_3': 'geo!-23.7424,-46.7066',
  'start_4': 'geo!-23.7424,-46.7066',
  'start_5': 'geo!-23.7424,-46.7066',
  'start_6': 'geo!-23.7424,-46.7066',
  'start_7': 'geo!-23.7424,-46.7066',
  'start_8': 'geo!-23.7424,-46.7066',
  'start_9': 'geo!-23.7424,-46.7066',
  'start_10': 'geo!-23.7424,-46.7066'},
 {'start_1': 'geo!-23.7424,-46.7066',
  'start_2': 'geo!-23.7424,-46.7066',
  'start_3': 'geo!-23.7424,-46.7066',
  'start_4': 'geo!-23.7424,-46.7066',
  'start_5': 'geo!-23.7424,-46.7066',
  'start_6': 'geo!-23.7424,-46.7066',
  'start_7': 'geo!-23.7424,-46.7066',
  'start_8': 'geo!-23.7424,-46.7066',
  'start_9': 'geo!-23.7424,-46.7066'}]
Related