我有一個看起來像這樣的小資料框:
place id, geometry,
AXRT {'location': {'lat': 51.112278, 'lng': 20.747889}}}}

我想得到這樣的東西:

我努力了:
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]})
但我收到一個錯誤:
TypeError: string indices must be integers
并且無法通過。
任何幫助將非常感激。
uj5u.com熱心網友回復:
IUC:
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}}]})
uj5u.com熱心網友回復:
可以apply(pd.Series)用來擴充字典。
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)
輸出:
place id lat lng
0 AXRT 51.112278 20.747889
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/389659.html
上一篇:用不同顏色為1000行繪制兩列
