将函数应用于 pandas 数据框

Applying function to pandas dataframe

我有一个名为 'tourdata' 的 pandas 数据框,由 676k 行数据组成。其中两列是纬度和经度。

使用 reverse_geocode 包我想将这些坐标转换为国家/地区数据。

当我打电话时:

import reverse_geocode as rg

tourdata['Country'] = rg.search((row[tourdata['latitude']],row[tourdata['longitude']]))

我收到错误:

ValueErrorTraceback (most recent call last) in () 1 coordinates = (tourdata['latitude'],tourdata['longitude']), ----> 2 tourdata['Country'] = rg.search((row[tourdata['latitude']],row[tourdata['longitude']]))

~/anaconda/envs/py3/lib/python3.6/site-packages/reverse_geocode/init.py in search(coordinates) 114 """ 115 gd = GeocodeData() --> 116 return gd.query(coordinates) 117 118

~/anaconda/envs/py3/lib/python3.6/site-packages/reverse_geocode/init.py in query(self, coordinates) 46 except ValueError as e: 47 logging.info('Unable to parse coordinates: {}'.format(coordinates)) ---> 48 raise e 49 else: 50 results = [self.locations[index] for index in indices]

~/anaconda/envs/py3/lib/python3.6/site-packages/reverse_geocode/init.py in query(self, coordinates) 43 """ 44 try: ---> 45 distances, indices = self.tree.query(coordinates, k=1) 46 except ValueError as e: 47 logging.info('Unable to parse coordinates: {}'.format(coordinates))

ckdtree.pyx in scipy.spatial.ckdtree.cKDTree.query()

ValueError: x must consist of vectors of length 2 but has shape (2, 676701)

测试包是否正常工作:

coordinates = (tourdata['latitude'][0],tourdata['longitude'][0]),
results = (rg.search(coordinates))
print(results)

输出:

[{'country_code': 'AT', 'city': 'Wartmannstetten', 'country': 'Austria'}]

感谢您对此的任何帮助。理想情况下,我想访问生成的字典并仅将国家/地区代码应用于国家/地区列。

搜索方法需要一个坐标列表。要获取单个数据点,您可以使用 "get" 方法。

尝试:

tourdata['country'] = tourdata.apply(lambda x: rg.get((x['latitude'], x['longitude'])), axis=1)

对我来说效果很好:

import pandas as pd
tourdata = pd.DataFrame({'latitude':[0.3, 2, 0.6], 'longitude':[12, 5, 0.8]})
tourdata['country'] = tourdata.apply(lambda x: rg.get((x['latitude'], x['longitude'])), axis=1)
tourdata['country']

输出:

0    {'country': 'Gabon', 'city': 'Booué', 'country...
1    {'country': 'Sao Tome and Principe', 'city': '...
2    {'country': 'Ghana', 'city': 'Mumford', 'count...
Name: country, dtype: object