作者localhostphp (A动动)
看板Python
标题Re: [问题]pandas重复分割,重复存档
时间Sun Apr 12 00:57:45 2015
※ 引述《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的地方
我只有测试两个,参考看看
欢迎一起讨论,精进彼此的技术:)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 122.116.217.21
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1428771468.A.C2D.html
1F:推 allen511081: 感谢你,效果是我要的,但是还是会有相同问题 04/12 10:28
2F:→ allen511081: .loc那行放入for loop後,即使我for设定2500, 04/12 10:31
3F:→ allen511081: 还是会出现[Errno 10060]的错误,另外我是要重复存回 04/12 10:32
4F:→ allen511081: CSV,所以只要重复存回CSV,之前的资料一样会被覆盖 04/12 10:34