
1、对数据进行清理,主要是度分秒的符号问题,有的是中文或者英文,需要统一替换成一种在Excel中完成。
2、把度分秒的数字提取出来,分别处理。
在此使用split函数,或正则表达式,看看自己,我在此使用正则表达式。
3、格式化使用format函数。
例如保留两位使用:02d。
实例
1 2 3 4 5 6 7 8 9 10 11 12 | import pandas as pd
import re
df = pd.read_excel( '1.xlsx' , sheet_name = 'Sheet1' )
def get(x):
degree = re.findall( '(\d+)°' , x)[0]
minute = re.findall( '° (\d+)' , x)[0]
second = re.findall( '' '' (.*?) "''', x)[0]
return '''{}°{:02d}'{:05.2f}" N' '' .format(degree, int(minute), float(second))
df[ 'lat' ] = df[ '纬度' ].apply(get)
df
|
以上就是python格式化经纬度的方法,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。