Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 运行location latitude and longitude函数以在数据帧上获取城市和国家_Python 3.x_Pandas_Latitude Longitude_Geopandas - Fatal编程技术网

Python 3.x 运行location latitude and longitude函数以在数据帧上获取城市和国家

Python 3.x 运行location latitude and longitude函数以在数据帧上获取城市和国家,python-3.x,pandas,latitude-longitude,geopandas,Python 3.x,Pandas,Latitude Longitude,Geopandas,当给定纬度和经度的单独坐标时,有一个函数可以很好地工作 from geopy.geocoders import Nominatim geolocator = Nominatim(user_agent="geoapiExercises") def city_state_country(coord): location = geolocator.reverse(coord, exactly_one=True) address = location.raw['add

当给定纬度和经度的单独坐标时,有一个函数可以很好地工作

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")
def city_state_country(coord):
    location = geolocator.reverse(coord, exactly_one=True)
    address = location.raw['address']
    city = address.get('city', '')
    state = address.get('state', '')
    country = address.get('country', '')
    return city, state, country
print(city_state_country("30.930508, 75.8419883"))
我想在我的dataframe中运行这个函数,并希望像这样在下一列中获得state的输出

ID  latitude     longitude       
1    30.930508    75.8419883     
2     Nan          Nan
3    13.1674503   80.2051151
所需内容对应于函数应提供的每个ID、纬度和经度,并给出如下输出:


注意-函数只接受一个逗号分隔的纬度和经度参数

ID  latitude     longitude     Location     
1    30.930508    75.8419883    state1 
2     Nan          Nan          no coordinate
3    13.1674503   80.2051151    state2
更新代码

import pandas as pd

df = pd.read_excel('C:\\....\\Latitude longitude detail.xlsx')

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="geoapiExercises")


def city_state_country(coord):
    try:
        location = geolocator.reverse(coord, exactly_one=True)
        address = location.raw['address']
        city = address.get('city', '')
        state = address.get('state', '')
        country = address.get('country', '')
        return city, state, country
#print(city_state_country("30.930508, 75.8419883"))

    except ValueError:
     return(0)

df['Location'] = (df[['latitude', 'longitude']].astype(str)
                   .apply(lambda row: city_state_country(', '.join(row)),
                          axis=1)
                 )
上述代码不运行。请指出错误 任何线索都会有帮助。谢谢

IIUC

df['Location'] = (df[['latitude', 'longitude']].astype(str)
                   .apply(lambda row: city_state_country(', '.join(row)),
                          axis=1)
                 )

函数只接受一个用逗号分隔的纬度和经度参数,所以两列的浮点数都应该用逗号分隔。请在执行后根据您的建议立即检查它是否抛出了ValueError。一旦做了异常处理,它就会无限期地运行。更新问题中的代码请查看。检查
df[['latitude','longitude']]。astype(str)。apply(','.join)
考虑到您的建议,并在脚本运行时调整我的脚本,代码运行并获取2200行的位置花费了近20分钟,这确实是一个时间复杂的脚本,但它运行得非常好