将纬度和经度列添加到现有数据框
Add latitude and longitude column to existing data frame
我无法将纬度和经度列添加到现有数据框中。我使用以下代码从我的数据集中的 Restaurant_Location 列之一中提取了坐标。
location = [x for x in rest_df['Restaurant_Location'].unique().tolist() if type(x) == str]
latitude = []
longitude = []
for i in range(0, len(location)):
if(type(location[i]) == str):
ctr=0
while True:
try:
address = location[i] + ', Mumbai, India'
geolocator = Nominatim(user_agent="ny_explorer")
loc = geolocator.geocode(address)
latitude.append(loc.latitude)
longitude.append(loc.longitude)
print('The geographical coordinate of location are {}, {}.'.format(loc.latitude, loc.longitude))
except:
ctr+=1
if(ctr==7):
print(i)
latitude.append(address)
longitude.append(address)
break
continue
break
我能够得到想要的输出
The geographical coordinate of location are 19.1840129, 72.8412155.
The geographical coordinate of location are 19.0583358, 72.8302669.
但是在 运行 上面的代码成功后 rest_df.head()
没有显示 location_latitude & location_longitude 我的数据框中的列,我想将其添加到数据框中。我的数据框中还有其他几列。请让我知道我在哪里做错了吗?
这是一段应该有效的代码,我删除了一些东西,因为我不确定为什么,但是如果你在其中做其他事情,你仍然可以把它们放回去。 new_df
应该包含原始数据框中的所有行,并添加了您想要的两列。我无法测试,因为我没有你的数据,所以他们可能有错字,但想法是存在的
location = [x for x in rest_df['Restaurant_Location'].unique().tolist()
if type(x) == str]
latitude = []
longitude = []
for i in range(0, len(location)):
# remove things that does not seem usefull here
try:
address = location[i] + ', Mumbai, India'
geolocator = Nominatim(user_agent="ny_explorer")
loc = geolocator.geocode(address)
latitude.append(loc.latitude)
longitude.append(loc.longitude)
print('The geographical coordinate of location are {}, {}.'.format(loc.latitude, loc.longitude))
except:
# in the case the geolocator does not work, then add nan element to list
# to keep the right size
latitude.append(np.nan)
longitude.append(np.nan)
# create a dataframe with the locatio, latitude and longitude
df_ = pd.DataFrame({'Restaurant_Location':location,
'location_latitude': latitude,
'location_longitude':longitude})
# merge on Restaurant_Location with rest_df to get the column
new_df = rest_df.merge(df_, on='Restaurant_Location', how='left')
我无法将纬度和经度列添加到现有数据框中。我使用以下代码从我的数据集中的 Restaurant_Location 列之一中提取了坐标。
location = [x for x in rest_df['Restaurant_Location'].unique().tolist() if type(x) == str]
latitude = []
longitude = []
for i in range(0, len(location)):
if(type(location[i]) == str):
ctr=0
while True:
try:
address = location[i] + ', Mumbai, India'
geolocator = Nominatim(user_agent="ny_explorer")
loc = geolocator.geocode(address)
latitude.append(loc.latitude)
longitude.append(loc.longitude)
print('The geographical coordinate of location are {}, {}.'.format(loc.latitude, loc.longitude))
except:
ctr+=1
if(ctr==7):
print(i)
latitude.append(address)
longitude.append(address)
break
continue
break
我能够得到想要的输出
The geographical coordinate of location are 19.1840129, 72.8412155.
The geographical coordinate of location are 19.0583358, 72.8302669.
但是在 运行 上面的代码成功后 rest_df.head()
没有显示 location_latitude & location_longitude 我的数据框中的列,我想将其添加到数据框中。我的数据框中还有其他几列。请让我知道我在哪里做错了吗?
这是一段应该有效的代码,我删除了一些东西,因为我不确定为什么,但是如果你在其中做其他事情,你仍然可以把它们放回去。 new_df
应该包含原始数据框中的所有行,并添加了您想要的两列。我无法测试,因为我没有你的数据,所以他们可能有错字,但想法是存在的
location = [x for x in rest_df['Restaurant_Location'].unique().tolist()
if type(x) == str]
latitude = []
longitude = []
for i in range(0, len(location)):
# remove things that does not seem usefull here
try:
address = location[i] + ', Mumbai, India'
geolocator = Nominatim(user_agent="ny_explorer")
loc = geolocator.geocode(address)
latitude.append(loc.latitude)
longitude.append(loc.longitude)
print('The geographical coordinate of location are {}, {}.'.format(loc.latitude, loc.longitude))
except:
# in the case the geolocator does not work, then add nan element to list
# to keep the right size
latitude.append(np.nan)
longitude.append(np.nan)
# create a dataframe with the locatio, latitude and longitude
df_ = pd.DataFrame({'Restaurant_Location':location,
'location_latitude': latitude,
'location_longitude':longitude})
# merge on Restaurant_Location with rest_df to get the column
new_df = rest_df.merge(df_, on='Restaurant_Location', how='left')