Pandas DataFrame 更新列

Pandas DataFrame updating columns

我正在尝试通过我拥有的物理地址获取场地的经度和纬度。

为此,我使用了 GoogleMaps API。

在执行以下代码时,我一直在尝试将经度和纬度直接插入数据框的空列中。

    import pandas as pd
    import googlemaps
    
    locations = df['ADDR']
    df['lat'] = ""
    df['lng'] = ""
    
    i = 0
    for location in locations:
        i = i + 1
        try:
            print("%d indexing %s location" % (i, location))
            geo_location = maps.geocode(location)[0].get('geometry')
            print(geo_location['location']['lat'], geo_location['location']['lng'])
            df['lat'].append(geo_location['location']['lat'])
            df['lng'].append(geo_location['location']['lng'])
            print(df)
        
            except IndexError:
                print("Address was wrong...")
            except Exception as e:
                print("Unexpected error occurred.", e) 

当我执行它时,我得到“无法连接''类型的对象;只有 Series 和 DataFrame objs 是有效的”。

我最初以为这是因为当我从 GoogleMaps 获取信息时,它是 json 格式。

所以,我尝试添加以下内容

geo_lat = pd.to_Series(geo_location['location']['lat'], geo_location(['location']['lng'])

然后我得到一个错误“列表索引必须是整数或切片,而不是 str”。

任何人都可以提供一种方法来正确地将值插入物理地址 df['ADDR'] 旁边的 df['lat'], df['lng'] 中吗?

要在 DataFrame 中添加新列并更新其值,您可以在循环中使用它,而无需预先定义列:

for i, location in enumerate(locations):
    df.at[i, 'lat'] = geo_location['location']['lat']
    df.at[i, 'lng'] = geo_location['location']['lng']