Python 将数组转换为元组
Python converting array to tuple
我正在尝试在地图上使用 folium 创建一条折线。它需要纬度和经度才能转换为坐标以显示为直线,我在下面编写了代码,它显示错误为
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
请告诉我如何将它显示为元组,以便我可以在地图上显示它。提前致谢。
使用生成器表达式并将其转换为元组
t = tuple(e for e in array)
您的代码的第一块应该已经为您提供了一个数据框
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 = [(i,j) for i,j in zip(df.long,df.lat)]
这就是你所需要的。
我正在尝试在地图上使用 folium 创建一条折线。它需要纬度和经度才能转换为坐标以显示为直线,我在下面编写了代码,它显示错误为
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
请告诉我如何将它显示为元组,以便我可以在地图上显示它。提前致谢。
使用生成器表达式并将其转换为元组
t = tuple(e for e in array)
您的代码的第一块应该已经为您提供了一个数据框
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 = [(i,j) for i,j in zip(df.long,df.lat)]
这就是你所需要的。