2017-03-16 72 views
0

創建新列有3列A,B和C的一個大熊貓據幀的內部一個大熊貓數據幀的循環

Id Date  Latitude  Longitude 
9497  2017-03-03 44.149147 -70.230300 
914   2017-02-27 38.832256 -104.761086 

我想遍歷數據框和內創建一個新的列循環。

我試着用下面的代碼,但我得到錯誤「列未定義」。任何建議都會非常有幫助。

for index,row in df2.iterrows(): 
    value = Geohash.encode(row['Latitude'],row['Longitude'], precision=8) 
    df2.set_value(index,'Geohash',value) 

回答

2

您的循環之前創建柱:

df2['Geohash'] = -1 # or np.nan or whatever 

for index,row in df2.iterrows(): 
     value = Geohash.encode(row['Latitude'],row['Longitude'], precision=8) 
     df2.set_value(index,'Geohash',value) 
+0

感謝您的建議。當我嘗試這個時,我得到了錯誤「long literal(long)(10):'dry51v5g'」 – user3447653

+0

我使用df2 ['Geohash'] =''解決了它。 – user3447653

+0

使用[.assign](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.assign.html)可能是更好的方法。 – ChuHo