簡體   English   中英

Python 將兩行合並為單行

[英]Python combine two two rows to single row

我正在嘗試在大葉地圖上創建折線。 為此,它需要一組坐標作為列表

它讀取以下 [[]] 之間的一組坐標作為一組 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' 並仍將其顯示為浮點數或列表

要將數據框的行作為列表,您可以簡單地取值

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM