我从Google Maps api获得了海拔数据,该数据输出了这样的数据框。
elevation location
0 996.330994 {'lat': 32.1897, 'lng': -103.18327}
1 995.314880 {'lat': 32.1894, 'lng': -103.18237}
2 994.983276 {'lat': 32.18729, 'lng': -103.18365}
3 995.970520 {'lat': 32.18416, 'lng': -103.1822}
4 997.625549 {'lat': 32.19115, 'lng': -103.18418}
我想将位置列分为纬度和经度两个单独的列
我尝试做
df ['location'] = df ['location']。astype('str')
df ['location'] = df ['location']。str.strip('{}')
它返回我
elevation location
0 996.330994 'lat': 32.1897, 'lng': -103.18327
1 995.314880 'lat': 32.1894, 'lng': -103.18237
2 994.983276 'lat': 32.18729,'lng': -103.18365
3 995.970520 'lat': 32.18416,'lng': -103.1822
4 997.625549 'lat': 32.19115,'lng': -103.18418
无法超越。
答案 0 :(得分:2)
在重建字典类型列之后使用concat
pd.concat([df,pd.DataFrame(df.location.tolist(),index=df.index)],axis=1)
Out[234]:
elevation location lat lng
0 1 {'lat': 32.1897, 'lng': -103.18327} 32.1897 -103.18327
1 2 {'lat': 32.1894, 'lng': -103.18237} 32.1894 -103.18237
答案 1 :(得分:1)
鉴于location中的值是字典,请尝试
df[['lat', 'lng']] = df.location.apply(pd.Series)
elevation location lat lng
0 996.330994 {'lat': 32.1897, 'lng': -103.18327} 32.18970 -103.18327
1 995.314880 {'lat': 32.1894, 'lng': -103.18237} 32.18940 -103.18237
2 994.983276 {'lat': 32.18729, 'lng': -103.18365} 32.18729 -103.18365
3 995.970520 {'lat': 32.18416, 'lng': -103.1822} 32.1841 -103.18220
4 997.625549 {'lat': 32.19115, 'lng': -103.18418} 32.19115 -103.18418