Re: [問題]pandas重複分割,重複存檔

看板Python作者 (A動動)時間10年前 (2015/04/12 00:57), 編輯推噓1(103)
留言4則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《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://www.ptt.cc/bbs/Python/M.1428771468.A.C2D.html

04/12 10:28, , 1F
感謝你,效果是我要的,但是還是會有相同問題
04/12 10:28, 1F

04/12 10:31, , 2F
.loc那行放入for loop後,即使我for設定2500,
04/12 10:31, 2F

04/12 10:32, , 3F
還是會出現[Errno 10060]的錯誤,另外我是要重複存回
04/12 10:32, 3F

04/12 10:34, , 4F
CSV,所以只要重複存回CSV,之前的資料一樣會被覆蓋
04/12 10:34, 4F
文章代碼(AID): #1LALACmj (Python)
文章代碼(AID): #1LALACmj (Python)