我有一个地理定位功能,我想将其应用于通过使用其他一些功能获得的不同的熊猫数据框。 我想做这样的事情,但我不知道怎么做:
deadly.apply(geolocation())
这是读取熊猫的代码:
import pandas as pd
import math
names = ["ID", "Vreme", "E", "N", "Ishod", "Tip", "Opis"]
my_dataFrame = pd.read_csv("SaobracajBeograd.csv", header = None, names = names)
这是我要应用的功能:
def geolocation():
#extract only coordinates and convert them to tuple
coordinates_data = my_dataFrame[["N", "E"]]
coordinates_data = coordinates_data.apply(tuple, axis=1)
#add tuple coordinates to list
coordinate_list = []
for coord in coordinates_data:
coordinate_list.append(coord)
#defult map location
gmap = gmplot.GoogleMapPlotter(44.797140, 20.513460, 11)
# Scatter points
coordinate_lats, coordinate_lons = zip(*coordinate_list)
gmap.scatter(coordinate_lats, coordinate_lons, 'red', size = 150, marker = False)
# Draw
gmap.draw("my_map1.html")
#geolocation()
这是为我提供要用于应用该功能的数据框的功能,以及该功能的结果:
def accidents_with_deadly_outcome():
deadly = my_dataFrame[my_dataFrame["Ishod"] == "Sa poginulim"]
deadly = deadly[["Ishod", "E", "N"]].reset_index(drop=True)
return deadly
#print(accidents_with_deadly_outcome())
accidents_with_deadly_outcome():
函数的结果:
0 Sa poginulim 20.657460 44.490140
1 Sa poginulim 20.654150 44.468720
2 Sa poginulim 20.536690 44.787231
3 Sa poginulim 20.510760 44.652443
4 Sa poginulim 20.510540 44.884460
5 Sa poginulim 20.471758 44.788613
6 Sa poginulim 20.359841 44.742405
7 Sa poginulim 20.472120 44.763520
8 Sa poginulim 20.610525 44.676290