【问题标题】:Python combine two two rows to single rowPython将两个两行组合成单行
【发布时间】:2019-12-06 00:07:56
【问题描述】:

我正在尝试在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”列中添加方括号并仍将其显示为浮点数或列表

【问题讨论】:

    标签: python list merge group-by folium


    【解决方案1】:

    要将数据框的行作为列表获取,您可以简单地获取值

    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],
    # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 2021-06-16
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多