在Python中从字符串中提取坐标
我的数据框中有此列在Python中从字符串中提取坐标,python,regex,Python,Regex,我的数据框中有此列 df['Y'] 0 -37.025.753 1 -37.024.621 2 -37.056.739 ... 214 -3.702.631 215 -37.025.369 Name: Longitud, Length: 216, dtype: object 1.我试过这个: df['Y'] = df.Y.str.rsplit(".",1).str[0] 我明白了: df['Y'] 0
df['Y']
0 -37.025.753
1 -37.024.621
2 -37.056.739
...
214 -3.702.631
215 -37.025.369
Name: Longitud, Length: 216, dtype: object
1.我试过这个:
df['Y'] = df.Y.str.rsplit(".",1).str[0]
我明白了:
df['Y']
0 -37.025
2.我试过这个:
df["Y"] = df["Y"].str.extract("(.+)\.[0-9]+")
我明白了:
df['Y']
0 -37.025
但我想将float中的数据类型更改为8位小数,如下所示:
df['Y']
0 -37.025753
您可以使用更明确的模式
df['Y'] = df['Y'].str.extract(r"(-*[0-9]+\.[0-9]+\.[0-9]+)")
#0 -37.025.753
然后删除不需要的句号,并转换为float:
df['Y'] = df['Y'].str.replace(r"\.([0-9]{3}$)",r"\1").astype('float64')
#0 -37.025753
您可以使用更明确的模式
df['Y'] = df['Y'].str.extract(r"(-*[0-9]+\.[0-9]+\.[0-9]+)")
#0 -37.025.753
然后删除不需要的句号,并转换为float:
df['Y'] = df['Y'].str.replace(r"\.([0-9]{3}$)",r"\1").astype('float64')
#0 -37.025753
@雷克斯:不客气!如果答案对你有帮助,你也能投票吗?看@RaqS,不客气!如果答案对你有帮助,你也能投票吗?看见