2016-12-30 207 views
1
Lon_X  Lat_Y 
5,234234  6,3234234 
5,234234  6,3234234 
5,234234  6,3234234 
5,234234  6,3234234 
5,234234  6,3234234 

我在上面的熊貓/數據框中有GPS座標。然而,這些使用逗號分隔符。使用熊貓將這些轉換爲浮動GPS座標的最佳方法是什麼?替換數據幀中的值Python

for item in frame.Lon_X: 
    float(item.replace(",", ".")) # makes the conversion but does not store it back 

我已經試過iteritems功能,但似乎很慢,給我一個警告,我真的不明白:

for index, value in frame.Lon_X.iteritems(): 
    frame.Lon_X[index] = float(value.replace(",", ".")) 

見警告文檔中: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 從ipykernel進口kernelapp爲app

回答

0

您可以使用applymap

df[["Lon_X", "Lat_Y"]] = df[["Lon_X", "Lat_Y"]].applymap(lambda x: float(x.replace(",", "."))) 
df 

enter image description here


以下是有關這些替代一些基準測試軟件,to_float_inplace是顯著比所有其他方法更快:

數據

df = pd.DataFrame({"Lon_X": ["5,234234" for i in range(1000000)], "Lat_Y": ["6,3234234" for i in range(1000000)]}) 

# to_float_inplace 
def to_float_inplace(x): 
    x[:] = x.str.replace(',', '.').astype(float) 

%timeit df.apply(to_float_inplace) 
# 1 loops, best of 3: 269 ms per loop 

# applymap + astype 
%timeit df.applymap(lambda x: x.replace(",", ".")).astype(float) 
# 1 loops, best of 3: 1.26 s per loop 

# to_float 
def to_float(x): 
    return x.str.replace(',', '.').astype(float) 

%timeit df.apply(to_float) 
# 1 loops, best of 3: 1.47 s per loop 

# applymap + float 
%timeit df.applymap(lambda x: float(x.replace(",", "."))) 
# 1 loops, best of 3: 1.75 s per loop 

# replace with regex 
%timeit df.replace(',', '.', regex=True).astype(float) 
# 1 loops, best of 3: 1.79 s per loop 
0

試試這個:

df.applymap(lambda x: float(x.replace(",", "."))) 

編輯:忘了map作爲@Psidom顯示

1

您可以將熊貓的量化方法一起就地一軸:

def to_float_inplace(x): 
    x[:] = x.str.replace(',', '.').astype(float) 

df.apply(to_float_inplace) 
0

你可以跳過使用申請,並直接與更換replace方法regex=True

df.replace(',', '.', regex=True).astype(float)