简体   繁体   中英

How to extract specific keys for location to dataframe

I have a small data frame which looks like that:

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

在此处输入图像描述

I would like to arrive at something like that: 在此处输入图像描述

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.

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}}]})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM