Pandas:DataFrame函数调用为所有行返回相同的结果

2024-09-30 00:36:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含以下列的数据框:

link    sqft    rent    bedroom     address1    address2    address3    bathroom

我有一个函数,它接受3个地址作为输入,然后以dict的形式返回纬度和经度

当我应用这样的函数时

df['lat'] = return_coordinates(df.address1,df.address2,df.addresse3).get('Latitude')

。。。lat列包含相同的值

下面是函数:

import herepy

geocoderApi = herepy.GeocoderApi('ap_key')
def return_coordinates(address1,address2,address3):
    response = geocoderApi.free_form('{},{},{}'.format(address1,address2,address3))
    geocode = response.as_dict()
    geocode = geocode.get('Response')
    geocode = geocode.get('View')
    value =  (geocode[0].get('Result')[0].get('Location').get('DisplayPosition'))
    return value

Tags: 函数dfgetreturnvalueresponsedictgeocode
2条回答

你的代码应该可以工作。以下是您正在寻找的示例,以防万一:

df = pd.DataFrame({'address1': [1, 2, 3], 'address2': [4, 5, 6], 'address3': [7, 8, 9]})

def return_coordinates(a,b,c):
    d = {
        "Latitude": a+b+c,
        "Longitude": a*b*c
    }
    return d

df['lat'] = return_coordinates(df['address1'], df['address2'], df['address3']).get("Latitude")
df['lng'] = return_coordinates(df['address1'], df['address2'], df['address3']).get("Longitude")
print(df)

输出:

   address1  address2  address3  lat  lng
0         1         4         7   12   28
1         2         5         8   15   80
2         3         6         9   18  162

下面是一个使用DataFrame.apply()函数的解决方案Documentation can be found here.

由于缺少数据和对API的访问,我假装您是数据集,尽管效果很差。但这演示了如何使用apply()函数从函数的返回值填充lat

基本(黑客)设置:

import pandas as pd

data = {'link': ['www.abc.com/1', 'www.abc.com/2', 'www.abc.com/3'],
        'sqft': [1111, 2222, 3333],
        'rent': ['$1111', '$2222', '$3333'],
        'bedroom': [1, 2, 3],
        'address1': [[34.052235, -118.243683], [33.052235, -117.243683], [32.052235, -115.243683]],
        'address2': [[32.715736, -117.161087], [31.715736, -116.161087], [30.715736, -115.161087]],
        'address3': [[33.541679, -117.777214], [32.541679, -116.777214], [31.541679, -115.777214]],
        'bathroom': [1, 2, 3]}

# Create dataset
df = pd.DataFrame(data)

def return_coordinates(address1, address2, address3):
    """Return the first value of the ``address1`` parameter."""
    return address1[0]

使用apply函数:

这将广播return_coordinates函数返回到数据帧的lat

df['lat'] = df.apply(lambda x: return_coordinates(x['address1'], x['address2'], x['address3']), axis=1)

原始数据集:

    address1    address2    address3    bathroom    bedroom     link    rent    sqft
0   [34.052235, -118.243683]    [32.715736, -117.161087]    [33.541679, -117.777214]    1   1   www.abc.com/1   $1111   1111
1   [33.052235, -117.243683]    [31.715736, -116.161087]    [32.541679, -116.777214]    2   2   www.abc.com/2   $2222   2222
2   [32.052235, -115.243683]    [30.715736, -115.161087]    [31.541679, -115.777214]    3   3   www.abc.com/3   $3333   3333

使用新的lat列:

address1    ...     lat
0   [34.052235, -118.243683]    ...     34.052235
1   [33.052235, -117.243683]    ...     33.052235
2   [32.052235, -115.243683]    ...     32.052235

从结果中可以看到,apply函数为每一行返回address1字段的第一个值

相关问题 更多 >

    热门问题