Home > Net >  How to place comma on interval value
How to place comma on interval value

Time:03-08

Here's my dataset

Id Longitude
1  923237487
2  102237487
3  934237487
4  103423787

What I did

df['Longitude'] = df['Longitude'].str.replace('\.', '', regex=True)
df['Longitude'] = (df['Longitude'].str[:3]   '.'   df['Longitude'].str[3:]).astype(float)

The result is

Id Longitude
1  923.237487
2  102.237487
3  934.237487
4  103.423787

My expected value, the value is between 80 - 160

Id Longitude
1  92.3237487
2  102.237487
3  93.4237487
4  103.423787

CodePudding user response:

IIUC, you could use a regex to find the leading numbers in the range 80-160:

df['Longitude2'] = (df['Longitude'].astype(str)
                    .str.replace(r'(^(?:1[0-5]|[8-9])[0-9])', r'\1.')
                    .astype(float)
                   )

output:

   Id  Longitude  Longitude2
0   1  923237487   92.323749
1   2  102237487  102.237487
2   3  934237487   93.423749
3   4  103423787  103.423787
  • Related