Python将数组转换为元组

问题描述 投票:0回答:2

我正在尝试使用地图上的叶子创建折线。它需要纬度和经度才能转换为坐标以显示为线,我在下面的代码中编写了它,将错误显示为

Location should consist of two numerical values, but array([ 46.931625, -84.52694 ]) of type <class 'numpy.ndarray'> is not convertible to float.

我的代码是


root = r'testfolder'
fstem = 'sample1'
fname = fstem+'.csv'
f = open(os.path.join(root,fname))
df= pd.read_csv(f)


points=[]
with open('testfolder/sample1.csv', 'r') as f:
    reader = csv.reader(f, delimiter=',')
    df['lat'] = df['lat'].astype(float)
    df['long'] = df['long'].astype(float)
    for i in f:
        points.append(tuple(df[['lat', 'long']].values))
        ave_lat = sum(p[0] for p in points)/len(points)
        ave_lon = sum(p[1] for p in points)/len(points)
tuple(map(tuple, points))

my_map = folium.Map(location=[ave_lat, ave_lon], zoom_start=14)

#add a markers
for each in points:  
    folium.Marker(each).add_to(my_map)

#fadd lines
folium.PolyLine(points, color="red", weight=2.5, opacity=1).add_to(my_map)


我的数据如下

event_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

[请让我知道如何将其显示为元组,以便可以在地图上显示它。预先感谢。

python arrays tuples folium
2个回答
0
投票

使用生成器表达式并将其转换为元组

t = tuple(e for e in array)

0
投票

代码的第一块应该已经给您一个数据框

root = r'testfolder'
fstem = 'sample1'
fname = fstem+'.csv'
f = open(os.path.join(root,fname))
df= pd.read_csv(f)

获得数据框之后

#convert the datatype of long and lat columns
df.long = df.long.astype(float)
df.lat = df.lat.astype(float)

#convert long and lat pairs in to a list of tuples
t = [(a,b) for a,b in zip(df.long,df.lat)]

这就是您所需要的。

© www.soinside.com 2019 - 2024. All rights reserved.