将 pandas 系列中的纬度/经度坐标转换为列表列表 [英] Convert lat/long coordinates in a pandas Series to list of lists
问题描述
我有一个名为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'] 转换为列表列表?
这是一个 head()...
坐标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 ...
假设您展示的是 Coords
列的摘录,您可以使用 pd.Series.str.split代码>:
coords = df.Coords打印(坐标)0 -88.12166374975578 42.130197892090251 -88.12166297898594 42.1300772827968262 -88.12166229779616 42.129970737404383 -88.12165682902426 42.1291142085465254 -88.12165440666122 42.12867029753218数据类型:对象list_ = coords.str.split(expand=True).applymap(float).values.tolist()打印(列表_)[[-88.12166374975578, 42.13019789209025],[-88.12166297898594, 42.130077282796826],[-88.12166229779616, 42.12997073740438],[-88.12165682902426, 42.129114208546525],[-88.12165440666122, 42.12867029753218]]
<小时>
编辑的解决方案:
打印(坐标)坐标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()打印)[[-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]]
I have a column in pandas called 'coords'. It has multiple comma delimited longitude + 'space' + latitude values in each row. A sample row for the 'coords' column would appear like below...
[-88.12166374975578 42.13019789209025, -88.12166297898594 42.130077282796826, -88.12166229779616 42.12997073740438, -88.12165682902426 42.129114208546525, -88.12165440666122 42.12867029753218]
I would like to create a list of lists from the list. So that it would appear like this...
[[-88.12166374975578, 42.13019789209025], [-88.12166297898594 ,42.130077282796826], [-88.12166229779616, 42.12997073740438], [-88.12165682902426,42.129114208546525], [-88.12165440666122, 42.12867029753218]]
How can I convert df['coords'] to the list of lists?
Here is a head()...
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...
Assuming what you showed is an excerpt of the Coords
column, you can use 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]]
Edited solution:
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]]
这篇关于将 pandas 系列中的纬度/经度坐标转换为列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!