从经度和纬度获取国家名称

4
我很愿意从我的数据框中的纬度和经度中提取位置的国家名称。
以下是我的数据样本:
{'Country/Region': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan},
         'Lat': {0: '33.93911',
          1: '41.1533',
          2: '28.0339',
          3: '42.5063',
          4: '-11.2027'},
         'Long': {0: '67.709953',
          1: '20.1683',
          2: '1.6596',
          3: '1.5218',
          4: '17.8739'},
         'Province/State': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan}}

注意:这是一个 pandas dataframe,我只是将其转换为字典来提问。
这是我尝试过的内容:
df[['Lat','Long']].apply(lambda x,y : geocoder.osm([x,y], method='reverse'),axis=1)

我收到了以下错误信息:

TypeError: ("<lambda>() missing 1 required positional argument: 'y'", 'occurred at index 0')

我认为你的lambda应该接受两个参数? - SuperStormer
2
df[['Lat','Long']].apply(lambda x,y : geocoder.osm([x,y], method='reverse'), axis=1)? 请注意,这是程序相关的内容。 - Quang Hoang
@SuperStormer 感谢你注意到这个问题,已经编辑过了。 - Emm
df[['Lat','Long']].apply(lambda x : geocoder.osm(list(x), method='reverse'), axis=1) - Quang Hoang
1
你需要阅读apply的文档,并对其工作方式的基本假设进行一些基本测试。 - Denziloe
显示剩余2条评论
1个回答

2

x 在你的lambda表达式中代表来自数据框的一行,这就是你需要做的事情。

df.apply(lambda row : geocoder.osm([row['Lat'], row['Long']], method='reverse'), axis=1)

如果你的数据集很大,考虑使用。请参考pandarallel

1
实际上,你现在可以删除[['Lat','Long']] - Denziloe
谢谢,出于好奇,为什么在处理较大的数据库时速度非常慢? - Emm
1
@Emm 在 pandas 中,apply 是按顺序运行的,因此您不会利用所有的核心。如果您正在使用公共地理编码服务,请注意不要超过限制。 - Jimmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接