将函数应用于数据框中的每个观察值

时间:2019-04-16 17:24:19

标签: python pandas geocoding

我要通过函数(反向地址解析器)放置较大的df坐标, 如何在不重复的情况下遍历整个df(需要很长时间)

示例df:

    Latitude    Longitude  
0   -25.66026   28.0914    
1   -25.67923   28.10525    
2   -30.68456   19.21694    
3   -30.12345   22.34256    
4   -15.12546   17.12365 

运行完我想要的功能后(没有for循环...)df:

     City
0    HappyPlace
1    SadPlace
2    AveragePlace
3    CoolPlace
4    BadPlace

注意:我不需要知道如何进行反向地理编码,这是一个将函数应用于整个df而无需迭代的问题。

编辑:

使用df.apply()可能无法正常工作,因为我的代码如下所示:

for i in range(len(df)):
    results = g.reverse_geocode(df['LATITUDE'][i], df['LONGITUDE'][i])
    city.append(results.city)

1 个答案:

答案 0 :(得分:2)

较慢的方法遍历地理位置列表并获取地理位置的城市

import pandas as pd
import time
d = {'Latitude': [-25.66026,-25.67923,-30.68456,-30.12345,-15.12546,-25.66026,-25.67923,-30.68456,-30.12345,-15.12546], 'Longitude': [28.0914, 28.10525,19.21694,22.34256,17.12365,28.0914, 28.10525,19.21694,22.34256,17.12365]}   
df = pd.DataFrame(data=d)
# example method of g.reverse_geocode() -> geo_reverse
def geo_reverse(lat, long):
    time.sleep(2)
    #assuming that your reverse_geocode will take 2 second
    print(lat, long)
for i in range(len(df)):
    results = geo_reverse(df['Latitude'][i], df['Longitude'][i])

由于time.sleep(2)。上述程序将至少花费20秒来处理所有十个地理位置。

比上述方法更好的方法:

import pandas as pd
import time
d = {'Latitude': [-25.66026,-25.67923,-30.68456,-30.12345,-15.12546,-25.66026,-25.67923,-30.68456,-30.12345,-15.12546], 'Longitude': [28.0914, 28.10525,19.21694,22.34256,17.12365,28.0914, 28.10525,19.21694,22.34256,17.12365]}   
df = pd.DataFrame(data=d)

import threading
def runnable_method(f, args):
    result_info = [threading.Event(), None]
    def runit():
        result_info[1] = f(args)
        result_info[0].set()
    threading.Thread(target=runit).start()
    return result_info

def gather_results(result_infos):
    results = []
    for i in range(len(result_infos)):
        result_infos[i][0].wait()
        results.append(result_infos[i][1])
    return results

def geo_reverse(args):
    time.sleep(2)
    return "City Name of ("+str(args[0])+","+str(args[1])+")"
geo_points = []
for i in range(len(df)):
    tuple_i = (df['Latitude'][i], df['Longitude'][i])
    geo_points.append(tuple_i)

result_info = [runnable_method(geo_reverse, geo_point) for geo_point in geo_points]
cities_result = gather_results(result_info)  
print(cities_result)

请注意,方法geo_reverse的处理时间为2秒,可根据地理位置获取数据。在第二个示例中,该代码仅需 2秒即可处理任意数量的点。

注意:尝试两种方法都假定您的geo_reverse将花费大约。 2秒以获取数据。第一种方法将花费20 + 1秒,处理时间将随着输入数量的增加而增加,但是第二种方法将具有几乎恒定的处理时间(即大约2 + 1)秒,无论您要处理多少个地理点。

假设上述代码中的g.reverse_geocode()方法为geo_reverse()分别运行上面的两个代码(方法),并自己查看差异。

说明: 看一下上面的代码及其主要部分,即创建元组列表并理解该列表,将每个元组传递给动态创建的线程(主要部分):

#Converting df of geo points into list of tuples
geo_points = []
for i in range(len(df)):
    tuple_i = (df['Latitude'][i], df['Longitude'][i])
    geo_points.append(tuple_i)
#List comprehension with custom methods and create run-able threads
result_info = [runnable_method(geo_reverse, geo_point) for geo_point in geo_points]
#gather result from each thread.
cities_result = gather_results(result_info)  
print(cities_result)