Re: [问题]pandas重复分割,重复存盘

楼主: localhostphp (A动动)   2015-04-12 00:57:45
※ 引述《allen511081 (蓝)》之铭言:
: 各位好,小弟最近用pandas遇到了问题,我有一个资料CSV档
: ,现在想做两件事
: 第一就是就是重复取出里面地点的资料,但是每一个循环就是300笔资料,
: 然后给Google api 去取得经纬度
: 第二就是,取得经纬度后,重复存回CSV档,这里我试过,会被覆蓋掉,
: 亦即当300~600笔资料取得经纬后存回CSV,会把前面1~300笔资料给盖过去,
: 变成1~300的资料的经纬变回原本的空白,请问在pandas里,有办法做到这两件事吗?
: 有没有人可以指导我一下?
: 附上我的source code:https://gist.github.com/allen511081/00c01068cd14dd99f3dc
: Google api:https://gist.github.com/allen511081/e83062a9f520483ec798
: CSV:https://drive.google.com/open?id=0B6SUWnrBmDwSM044UGFIRUZkeGc&authuser=0
# -*- coding: utf-8 -*-
import pandas as pd
from geocodequery import GeocodeQuery
df = pd.read_csv('./birdsIwant3.csv',low_memory=False)
def addrs(location):
gq = GeocodeQuery("zh-tw", "tw")
gq.get_geocode(location)
print location
return pd.Series({"lat": gq.get_lat(), "lng": gq.get_lng()})
df['lat'] = 0
df['lng'] = 0
df.loc[1:3, ['lat','lng']] = df[1:3]['location'].apply(addrs) ##the problem##
df.loc[3:5, ['lat','lng']] = df[3:5]['location'].apply(addrs) ##the problem##
print df
# col_list = list(df)####
# col_list.insert(3,'lat')# insert column names at new positions
# col_list.insert(4,'lng')
# col_list=col_list[:-2]# slice off the last 2 values
# df = df.ix[:,col_list]# use ix and pass the new column order to sort the order
# df.to_csv('./birdsIwant3.csv',index=False)
不知道这样是不是你要的效果
不确定你是不是每次循环都要重复写入到csv
如果只是写回pandas dataframe的话,因为你每次循环都会把之前得洗掉
如果要改的话,要先指定你这次循环内要写入的位置
就是我上面的.loc的地方
我只有测试两个,参考看看
欢迎一起讨论,精进彼此的技术:)
作者: allen511081 (蓝)   2015-04-12 10:28:00
感谢你,效果是我要的,但是还是会有相同问题.loc那行放入for loop后,即使我for设定2500,还是会出现[Errno 10060]的错误,另外我是要重复存回CSV,所以只要重复存回CSV,之前的资料一样会被覆蓋

Links booklink

Contact Us: admin [ a t ] ucptt.com