python - 从Python中的字符串中提取坐标
问题描述
我的 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.我试过这个:
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
但我想用 8 位小数更改浮点中的 dtype,如下所示:
df['Y']
0 -37.025753
解决方案
您可以使用更明确的模式
df['Y'] = df['Y'].str.extract(r"(-*[0-9]+\.[0-9]+\.[0-9]+)")
#0 -37.025.753
然后删除您不想要的句号,并转换为浮动:
df['Y'] = df['Y'].str.replace(r"\.([0-9]{3}$)",r"\1").astype('float64')
#0 -37.025753
推荐阅读
- visual-studio-2017 - 如何在 cocos creator 中构建桌面(Windows 10、8.1、8)
- asp.net-core - 使用服务器端 Blazor 动态填充选择列表的问题
- asp.net - DotNet 部署流程
- mysql - 每次项目运行时,django 都会出现重复错误
- mysql - 如何解决 sum、length 和 group by 的问题
- hadoop - hadoop 运行不正常
- android - 如何每天在 sqlite 数据库中插入出勤率(可能是对或错)
- python - 从 pandas 数据帧的每一行中的单词中删除多个字符组合
- java - 格式化我的系统后未安装应用程序且未签名应用程序
- flutter - 展平飞镖中列表的动态列表