Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从字符串中提取坐标_Python_Regex - Fatal编程技术网

在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,不客气!如果答案对你有帮助,你也能投票吗?看见