问题描述
我是Python的初学者,我一直在使用Jupyter上的Pandas和Geocoder对数据库进行地理编码.
I'm a beginner at Python and I've been working to geocode a database using Pandas and Geocoder on Jupyter.
由于df有点长(大约3000行),所以我想使用Google的地理编码API.
Since the df is a little long (around 3000 rows), I'd like to use Google's Geocoding API.
我已经创建了一个自由密钥,但是我不知道该怎么做.帮助吗?
I've already created a free key, but I have no idea what I'm supposed to do with it. Help?
顺便说一句,我的代码如下:
By the way, my code looks like this:
import geocoder
import pandas as pd
geo = geocoder
df=pd.read_excel('hsp2.xlsx')
df['Coordinates']=df['Address'].apply(geo.google).apply(lambda x: x.latlng if x != None else None)
df.to_csv('output.csv', sep='|', encoding='iso8859_15')
推荐答案
在导入geocoder
之前,需要先设置环境变量 :
You need to set the environment variable before you import geocoder
:
import os
os.environ["GOOGLE_API_KEY"] = "api_key_from_google_cloud_platform"
import geocoder
geo = geocoder.google(address)
latlong = geo.latlng
注意:
正如Murmel在评论中提到的那样,不应在代码内部设置包含键(通常)的环境变量.
如果要将其部署在某处,请在配置文件中设置环境变量.甚至更好,作为Kubernetes之类的秘密.
As Murmel mentioned in the comments, environment variables containing keys (and in general) should not be set inside of your code.
If you are deploying this somewhere then set up enviroment variables in your configuration file. Or even better, as a secret in something like Kubernetes.
否则用
在bash中设置环境变量export GOOGLE_API_KEY=api_key_from_google_cloud_platform
Else set the environment variable in bash withexport GOOGLE_API_KEY=api_key_from_google_cloud_platform
这篇关于将我的Google Geocoding API密钥与Python geocoder一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!