简体   繁体   English

Python将数组转换为元组

[英]Python converting array to tuple

I am trying to create a poly line using folium on map.我正在尝试在地图上使用 folium 创建一条折线。 It needs lat and longitude to convert to coordinates to show as line, I have written below code and it shows error as它需要纬度和经度才能转换为坐标以显示为线,我写了下面的代码,它显示错误为

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

my code is我的代码是


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)


my data is as follows我的数据如下

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

Please let me know how can I show it as tuple so that I can show it on map.请让我知道如何将其显示为元组,以便我可以在地图上显示它。 Thanks in advance.提前致谢。

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

t = tuple(e for e in array)

The first block of your code should already give you a data frame代码的第一个块应该已经为您提供了一个数据框

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

After you get the dataframe获得数据框后

#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 = [(i,j) for i,j in zip(df.long,df.lat)]

That is all you need.这就是你所需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM