Home > Enterprise >  how add a point in an specific position of a string in a column in python
how add a point in an specific position of a string in a column in python

Time:11-30

Hy! I have a dataframe with two columns latitude and longitude with a wrong format that i want to correct. The structure of de strings in columns is the next

Lat Long
-314193332 -6419125129999990
-313147283 -641708031

I need to append a point in the third position to have this structure:

Lat Long
-31.4193332 -64.19125129999990
-31.3147283 -64.1708031

how can i do this?

the value being an integer type too, i can configure that if there is a function to edit integers

CodePudding user response:

You can use arithmetic with a conversion to log10 to get the number of digits:

N = 2 # number of digits to keep before decimal part
out = df.div(10**np.floor(np.log10(df.abs()) 1).sub(N))

Output:

         Lat       Long
0 -31.419333 -64.191251
1 -31.314728 -64.170803

Intermediate (number of digits):

np.floor(np.log10(df.abs()) 1)

   Lat  Long
0  9.0  16.0
1  9.0   9.0

CodePudding user response:

Another possible solution, which uses regex to replace the first two digits to the same digits plus .:

df.astype(str).replace(r'(^-?\d{2})',r'\1.', regex=True).astype(float)

Output:

         Lat       Long
0 -31.419333 -64.191251
1 -31.314728 -64.170803
  • Related