python - 在 pandas 中用反引号解码对象
问题描述
import pandas as pd
sr = pd.Series(["`1", "2", "`3", "4", None, None, None])
我有一个与上述对象非常相似的对象系列(len>10000)。我想保留非数字,但将数字转换为整数。我不确定如何处理似乎用反引号编码的数字。解决此问题的最佳方法是什么?
解决方案
这是一个可能的解决方案:
sr = sr.str.replace('`', '').astype('float').astype('Int32')
您不能将字符串转换为可为空的整数。解决方法是首先将其转换为浮点数。这是结果系列:
0 1
1 2
2 3
3 4
4 <NA>
5 <NA>
6 <NA>
dtype: Int32
推荐阅读
- c++ - thread_local 单例默认执行惰性初始化?
- javascript - React --> 警告:validateDOMNesting(...):
不能作为后代出现
- sql-server - qt到sql服务器连接
- r - 覆盖多个参数,更快的方法
- css - Angular Material - 拒绝应用样式
- c - 计算树和图来模拟 ND 图灵机
- asp.net-core-mvc - 在 windows 上开发 .net core 并在 linux 的 azure app 服务上部署
- android - 未解决的参考 NotificationCompat 即使在添加支持库之后
- jenkins - 与 Blue Ocean、Github 和 Nuget 的持续集成导致路径太长
- ios - 如何隐藏除地图应用程序以外的所有应用程序?