将pandas系列中的纬度/经度坐标转换为列表列表

时间:2017-08-25 04:13:03

标签: python pandas series

我在熊猫中有一个名为“coords'”的专栏。它有多个以逗号分隔的经度+空格'每行+纬度值。 ' coords'的一个示例行。列将显示如下......

[-88.12166374975578 42.13019789209025,  -88.12166297898594 42.130077282796826,  -88.12166229779616 42.12997073740438,  -88.12165682902426 42.129114208546525,  -88.12165440666122 42.12867029753218]

我想从列表中创建一个列表列表。所以它看起来像这样......

[[-88.12166374975578, 42.13019789209025],  [-88.12166297898594 ,42.130077282796826],  [-88.12166229779616, 42.12997073740438],  [-88.12165682902426,42.129114208546525],  [-88.12165440666122, 42.12867029753218]]

如何将df [' coords']转换为列表列表?

这是一个头()......

    coords
0   -88.12166374975578 42.13019789209025, -88.12166297898594 42.130077282796826, -88.12166229779616 42.12997073740438, -88.12165682902426 42.129114208546525, -88.12165440666122 42.12867029753218, -88.12165409167278 42.12861210461891, -88.12165078955562 42.1280072560737, -88.1216505237599 42.127958648542936, -88.12164976861018 42.127820070569165, -88.12164950156834 42.127770730347784, -88.12164936198349 42.127745113495685, -88.12164631909246 42.12698047923614, -88.12164465148149 42.126561239318384, -88.12164441208937 42.126501380826646, -88.12165535387125 42.125918676152615, -88.12165901489989 42.1257236125411, -88.12166910482216 42.125179681003004, -88.12167046792653 42.12511347549821, -88.12168153859359 42.124574951678966, -88.12169213266428 42.12405994975595, -88.12169609920953 42.123867...
1   -88.15806483536268 42.15423929791892, -88.15734814434225 42.15424023425998, -88.15692561771552 42.15424078182948, -88.15612280604331 42.15424182229812, -88.15570230201315 42.154247060953836, -88.15537304882349 42.15424548051985, -88.15424894139665 42.15424008174756, -88.15312432528388 42.15423466567452, -88.15200516375596 42.15422926640768, -88.15075402101326 42.1542232181898, -88.15074137162432 42.15422315689777, -88.15073738857417 42.15384470168878, -88.1507388608806 42.15329655518857, -88.15074017125366 42.15246856985761, -88.15074053615406 42.15224538180373, -88.15074152744889 42.151633597914206, -88.15074252669456 42.15055197422978, -88.15074334980639 42.15033614385567, -88.15074448165737 42.15003982848825, -88.15074567060333 42.14972749019171, -88.15074611950101 42.14952766024307...

1 个答案:

答案 0 :(得分:4)

假设您展示的内容是Coords列的摘录,您可以使用pd.Series.str.split

coords = df.Coords
print(coords)

0     -88.12166374975578 42.13019789209025
1    -88.12166297898594 42.130077282796826
2     -88.12166229779616 42.12997073740438
3    -88.12165682902426 42.129114208546525
4     -88.12165440666122 42.12867029753218
dtype: object


list_ = coords.str.split(expand=True).applymap(float).values.tolist()
print(list_)

[[-88.12166374975578, 42.13019789209025],
 [-88.12166297898594, 42.130077282796826],
 [-88.12166229779616, 42.12997073740438],
 [-88.12165682902426, 42.129114208546525],
 [-88.12165440666122, 42.12867029753218]]

编辑解决方案:

print(coords)

                                              coords
0  -88.12166374975578 42.13019789209025, -88.1216...
1  -88.15806483536268 42.15423929791892, -88.1573...

out = df.coords.str.split(',\s+').apply(pd.Series).stack()\
                  .str.split(expand=True).applymap(float).values.tolist()
print(out)

[[-88.12166374975578, 42.13019789209025],
 [-88.12166297898594, 42.130077282796826],
 [-88.12166229779616, 42.12997073740438],
 [-88.12165682902426, 42.129114208546525],
 [-88.12165440666122, 42.12867029753218],
 [-88.12165409167278, 42.12861210461891],
 [-88.12165078955562, 42.1280072560737],
 [-88.1216505237599, 42.127958648542936],
 [-88.12164976861018, 42.127820070569165],
 [-88.12164950156834, 42.127770730347784],
 [-88.12164936198349, 42.127745113495685],
 [-88.12164631909246, 42.12698047923614],
 [-88.12164465148149, 42.126561239318384],
 [-88.12164441208937, 42.126501380826646],
 [-88.12165535387125, 42.125918676152615],
 [-88.12165901489989, 42.1257236125411],
 [-88.12166910482216, 42.125179681003004],
 [-88.12167046792653, 42.12511347549821],
 [-88.12168153859359, 42.124574951678966],
 [-88.12169213266428, 42.12405994975595],
 [-88.12169609920953, 42.123867],
 [-88.15806483536268, 42.15423929791892],
 [-88.15734814434225, 42.15424023425998],
 [-88.15692561771552, 42.15424078182948],
 [-88.15612280604331, 42.15424182229812],
 [-88.15570230201315, 42.154247060953836],
 [-88.15537304882349, 42.15424548051985],
 [-88.15424894139665, 42.15424008174756],
 [-88.15312432528388, 42.15423466567452],
 [-88.15200516375596, 42.15422926640768],
 [-88.15075402101326, 42.1542232181898],
 [-88.15074137162432, 42.15422315689777],
 [-88.15073738857417, 42.15384470168878],
 [-88.1507388608806, 42.15329655518857],
 [-88.15074017125366, 42.15246856985761],
 [-88.15074053615406, 42.15224538180373],
 [-88.15074152744889, 42.151633597914206],
 [-88.15074252669456, 42.15055197422978],
 [-88.15074334980639, 42.15033614385567],
 [-88.15074448165737, 42.15003982848825],
 [-88.15074567060333, 42.14972749019171],
 [-88.15074611950101, 42.14952766024307]]