基本上,我有一个大约5,000个经度和纬度的列表,我想分别循环并分别运行此查询。我一直在手动执行此操作,并逐个运行查询,但是我想必须有一种标准化流程的方法。任何帮助或想法都将不胜感激,谢谢!
df = []
places_result = gmaps.places(query=query, location=(latitude1, longitude1), radius=32186.9, )
for place in places_result['results']:
my_place_id = place['place_id']
my_fields = ['name', 'business_status', 'formatted_address', 'opening_hours', 'rating', 'website',
'formatted_phone_number', 'geometry/location/lng', 'geometry/location/lat']
place_details = gmaps.place(place_id=my_place_id, fields=my_fields)
column_names = ['name', 'rating', 'address', 'website', 'phone', 'status', 'hours', 'lat', 'lng']
# df=pd.DataFrame(columns = column_names)
try:
rating = place_details['result']['rating']
except KeyError:
rating = 'na'
try:
name = place_details['result']['name']
except KeyError:
name = "na"
try:
address = place_details['result']['formatted_address']
except KeyError:
address = "na"
try:
website = place_details['result']['website']
except KeyError:
website = "na"
try:
phone = place_details['result']['formatted_phone_number']
except KeyError:
phone = "na"
try:
lat = place_details['result']['geometry']['location']['lat']
except KeyError:
lat = "na"
try:
lng = place_details['result']['geometry']['location']['lng']
except KeyError:
lng = "na"
try:
status = place_details['result']['business_status']
except KeyError:
status = "na"
try:
hours = place_details['result']['opening_hours']
except KeyError:
hours = 'na'
df1 = {"name": [name, ], "rating": [rating, ], "address": [address, ], "website": [website, ], "phone": [phone, ],
"status": [status, ], "hours": [hours, ], "lat": [lat, ], "lng": [lng, ]}
data: DataFrame = pd.DataFrame(data=df1)
df.append(data)
dfr = pd.concat(df)
答案 0 :(得分:0)
关于您的尝试,第一件事,除了块,您可以使用以下代码重写它们:
result = place_details['result']
rating = result.get('rating', 'na')
name = result.get('name', 'na')
...
,依此类推。 dict.get(key,default = None)方法将避免引发KeyError,并且您可以提供默认值'na'。
您可以通过字典理解来进一步简化:
keys = ["name", "rating", "address", "website", "phone",
"status", "hours", "lat", "lng"]
entry = {k: result.get(k, 'na') for k in keys}
假设您的纬度和经度值位于两个列表中:
entries = []
for lat, lon in zip(latitudes, longitudes):
q = gmaps.places(query=query, location=(lat, lon), radius=32186.9,)
result = q.get('result', {}) # returns an empty dict if the query fails
entry = {k: result.get(k, 'na') for k in keys}
entries.append(entry)
df = pd.DataFrame(entries) # create a pandas dataframe
答案 1 :(得分:0)
static struct Icon IconList[]= {
{1, "Offline", Animation::Hide, Animation::Hide, 1},
{2, "Training", Animation::Hide, Animation::Hide, 1},
{0, {0}, Animation::Hide, Animation::Hide, 1}
};