【发布时间】:2015-03-26 02:27:38
【问题描述】:
我想使用 Google API 在 CSV 文件中获取“位置”的纬度和经度,我可以通过 Google API 模块获取 'lat' 、 'lng' 。但我无法将文件保存回原始文件并在“位置”后面插入
我的原始文件如下所示:
date time location birdName count birdName count birdName count
1990-02-10 0900:1200 balabala bird1 15 bird2 10 bird3 20
1990-02-28 1300:1500 balabala bird4 40 bird5 10 bird6 25
1990-03-01 0900-1200 balabala bird7 45 bird8 15 bird9 30
... ... ... ... ... ... ... ... ...
我想在 'location' 之后插入 'lat' 和 'lng' 列,如下所示:
date time location lat lng birdName count birdName count birdName count
1990-02-10 0900:1200 balabala xxx xxx bird1 15 bird2 10 bird3 20
1990-02-28 1300:1500 balabala xxx xxx bird4 40 bird5 10 bird6 25
1990-03-01 0900-1200 balabala xxx xxx bird7 45 bird8 15 bird9 30
... ... ... ... ... ... ... ... ... ... ...
Google API 模块:https://drive.google.com/open?id=0B6SUWnrBmDwSb3BabFdEcXV3LUU&authuser=0
我的代码:
# -*- coding: utf-8 -*-
import pandas as pd
from geocodequery import GeocodeQuery
def addrs(location):
for addrs in location:
addr= addrs
gq = GeocodeQuery("zh-tw", "tw")
gq.get_geocode(addr)
lng=gq.get_lng()
lat=gq.get_lat()
df['lat']=lat
df['lng']=lng
df.to_csv('./birdsIwant.csv')
df = pd.read_csv('./birdsIwant.csv',low_memory=False)
addrs(df['location'])
我该怎么办?
【问题讨论】:
标签: python google-maps csv pandas