Pandas DataFrame updating columns



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

为此,我使用GoogleMaps API。

在执行以下代码时,我一直试图直接将经度和纬度插入到dataframe的空列中。

  • 在df['ADDR']中存储了1500个场馆的物理地址。
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) 

当我执行它的时候,我得到" cannot concatize type '';只有Series和DataFrame对象是有效的。

我首先想到这是因为当我从GoogleMaps获得信息时,它是json格式的。

因此,我尝试添加以下

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

然后我得到一个错误"列表索引必须是整数或切片,而不是字符串"。

有没有人可以提供一种方法来插入值到df['lat'], df['lng']旁边的物理地址df['ADDR']正确?

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

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