Home > other >  how to get only first two values ​from a dictionary list
how to get only first two values ​from a dictionary list

Time:12-13

I have a dataframe with several lists of dictionaries and I would like to get only the first value of "latitude" and "longitude" for each row and add the keys "latitude" and "longitude" as columns in the same dataframe. How to do this?image

CodePudding user response:

Something like this should work:

df['latitude'] = df['geo_points'].apply(lambda x: x[0]['latitude'])
df['longitude'] = df['geo_points'].apply(lambda x: x[0]['longitude'])

lambda is basically the same as defining a function

def lat(x):
    return x[0]['latitude']
  • Related