Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace values in dataframe Python

Tags:

python

pandas

Lon_X        Lat_Y
5,234234     6,3234234
5,234234     6,3234234
5,234234     6,3234234
5,234234     6,3234234
5,234234     6,3234234

I've GPS coordinates in a pandas/dataframe like above. These however use the comma separator. What's the best way using pandas to convert these to float GPS coordinates?

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

I've tried the iteritems function, but seems very slow and gives me a warning that I don't really understand:

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

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy from ipykernel import kernelapp as app

like image 827
Juan Avatar asked Aug 26 '26 14:08

Juan


1 Answers

You can use applymap:

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

enter image description here


Here is some benchmark about these alternatives, to_float_inplace is significantly faster than all other methods:

Data:

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
like image 88
Psidom Avatar answered Aug 29 '26 04:08

Psidom