【问题标题】:Getting country name from longitude and latitude从经度和纬度获取国家名称
【发布时间】:2020-08-10 21:43:00
【问题描述】:

我想从我的数据框中位置的纬度和经度中提取国家名称。

这是我的数据示例:

{'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 数据框,我只是为了提出这个问题而将其转换为字典。

这是我尝试过的:

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 应该使用 2 个参数?
  • df[['Lat','Long']].apply(lambda x,y : geocoder.osm([x,y], method='reverse'), axis=1)?
  • @QuangHoang 用你的建议编辑了我的答案,仍然收到类似的错误
  • @SuperStormer 感谢您注意到这一点,已编辑问题
  • 您需要阅读apply 的文档并对您对其工作方式的假设进行一些基本测试。

标签: python pandas


【解决方案1】:

您的 lambda 中的x 将是数据框中的一行,这是您需要做的

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

如果您的数据集很大,请考虑使用pandarallel

【讨论】:

  • 其实你现在可以放弃[['Lat','Long']]了。
  • 谢谢,出于好奇,为什么大型数据库会很慢?
  • @Emm apply 在 pandas 中按顺序运行,因此您不会使用所有内核,如果您使用公共地理编码服务,请注意不要达到限制
猜你喜欢
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-28
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多