Python将两两行合并为单行

时间:2019-12-06 00:07:56

标签: python list merge group-by folium

我正在尝试在叶片地图上创建折线。为此,它需要一组坐标作为列表

将下面[[]]之间的一组坐标读取为一组GPS坐标

进口大叶草

coordinates = [[42.408485,-83.4551716666667],[42.408485,-83.45517],
               [42.408485,-83.45516],[42.4084866666667,-83.4551416666667]], 
               [[42.4085016666667,-83.4547916666667],[42.4085016666667,-83.45479],[42.4085033333333,-83.4547533333333]]
# Create the map and add the line
m = folium.Map(location=[41.9, -97.3], zoom_start=4)
my_PolyLine=folium.PolyLine(locations=coordinates,weight=5)
map_US.add_children(my_PolyLine)

我有一个很大的文件,我正在尝试从文件中实现相同的文件

id  long    lat
a   -84.52694   46.931625
a   -84.52684   46.931725
a   -94.25526333    42.71689167
a   -94.25524667    42.71689333
a   -94.25519167    42.716895
a   -94.25505167    42.71690833
b   -94.25531167    42.71687167
b   -94.255205  42.71689
b   -94.25515   42.7169
b   -94.25507   42.71691167
b   -94.25507167    42.71691167
b   -94.25511   42.716905
b   -94.25514667    42.71689833
b   -94.25515667    42.71689667
b   -94.255165  42.716895
b   -94.25518167    42.71689
c   -94.25519167    42.71688833
c   -94.25522833    42.71688167
c   -94.25523833    42.71688
c   -94.25525   42.71688

所有GPS坐标必须组成一个列表并按id列分组,我的代码如下所示

import numpy as np
import folium
from folium import plugins
import pandas as pd
from folium.plugins import HeatMapWithTime
df_acc= pd.read_csv(f)
df_acc['lat_long'] = [', '.join(str(x) for x in y) for y in map(tuple, df_acc[['lat', 'long']].values)]
a=list(df_acc['lat_long'].head(10))
m = folium.Map(location=[41.9, -97.3], zoom_start=4)
my_PolyLine=folium.PolyLine(locations=a,weight=5)
m.add_children(my_PolyLine)
m

这没有给出任何结果,如何在方括号“ a”中添加方括号,并仍然将其显示为float或list

1 个答案:

答案 0 :(得分:0)

要获取数据框的行作为列表,您只需获取值

df_acc = pd.read_csv(f)
df
#    id       long        lat
# 0   a -84.526940  46.931625
# 1   a -84.526840  46.931725
# ...

a = df_acc[['lat', 'long']].values
# a
# array([[ 46.931625  , -84.52694   ],
#       [ 46.931725  , -84.52684   ],
# ...

您可以将数组a用作位置列表

要按ID分组:

df_acc.groupby(['id','long','lat']).sum().reset_index().values
# array([['a', -84.52694, 46.931625],
#        ['a', -84.52684, 46.931725],
# ...