Home > Blockchain >  How to extract specific keys for location to dataframe
How to extract specific keys for location to dataframe

Time:12-22

I have a small data frame which looks like that:

place id,    geometry,
AXRT      {'location': {'lat': 51.112278, 'lng': 20.747889}}}}

enter image description here

I would like to arrive at something like that: enter image description here

I have tried:

df=pd.DataFrame(data, columns=['geometry'])
df1 = pd.DataFrame({'Geometry': np.array([subdct['x'] for subdct in df], dtype='datetime64[s]'),
                   'y': [subdct['y'] for subdct in df]})

But I get an error:

TypeError: string indices must be integers

And cannot get through that.

Any help would be much appreciated.

CodePudding user response:

IIUC:

df = df[['place id']].join(df['location'].apply(pd.Series)['location'].apply(pd.Series))
print(df)

OUTPUT

  place id        lat        lng
0     AXRT  51.112278  20.747889

SETUP

df = pd.DataFrame(data={'place id':['AXRT'], 'location':[{'location': {'lat': 51.112278, 'lng': 20.747889}}]})

CodePudding user response:

You can use apply(pd.Series) to expand the dictionary.

df = pd.DataFrame({'place id': 'AXRT', 'geometry':{'location': {'lat': 51.112278, 'lng': 20.747889}}})
out = df[['place id']].merge(df['geometry'].apply(pd.Series), right_index=True, left_index=True).reset_index(drop=True)

Output:

  place id        lat        lng
0     AXRT  51.112278  20.747889
  • Related