[英]Extracting coordinates from a string in Python
I have this column in my DataFrame我的 DataFrame 中有这个专栏
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.I tried this: 1.我试过这个:
df['Y'] = df.Y.str.rsplit(".",1).str[0]
and I get this:我明白了:
df['Y']
0 -37.025
2.I tried this: 2.我试过这个:
df["Y"] = df["Y"].str.extract("(.+)\.[0-9]+")
and I get this:我明白了:
df['Y']
0 -37.025
But I would like change dtype in float with 8 decimal like this:但我想用 8 位小数改变浮点数中的 dtype,如下所示:
df['Y']
0 -37.025753
You could use a more explicit pattern您可以使用更明确的模式
df['Y'] = df['Y'].str.extract(r"(-*[0-9]+\.[0-9]+\.[0-9]+)")
#0 -37.025.753
then remove the full stop you don't want, and convert to float:然后删除您不想要的句号,并转换为浮动:
df['Y'] = df['Y'].str.replace(r"\.([0-9]{3}$)",r"\1").astype('float64')
#0 -37.025753
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.