Python geopy 接受手动字符串,但在循环 csv df 时不接受

Python geopy accepts manual string but not when looping through csv df

不确定为什么在我尝试获取位置信息时会发生这种情况。我的示例 csv 具有以下

address,city,state,zip
767 5th Ave, New York, NY, 10153

我的 python 文件看起来像这样,我在遇到问题的地方添加了评论

from geopy.geocoders import Nominatim
geolocator = Nominatim()
import pandas as pd

df = pd.read_csv('test.csv')

for index, row in df.iterrows():
    # doesnt work when looping
    location = geolocator.geocode(row['address'],row['city'],row['state'],row['zip'])

    # works manually
    #location = geolocator.geocode("767 5th Ave, New York, NY, 10153")
    print(location.raw)

这些不一样吗?

我是这样解决的

for index, row in df.iterrows():
    street = str(row['address'])
    city = str(row['city'])
    state = str(row['state'])
    zipcode = str(row['zip'])
    location = geolocator.geocode("%s %s %s %s" % (street,city,state,zipcode))
    print(location.raw)

让我知道这是否是最有效的方法,干杯