python - 如何使用 Pandas 仅用值中的整数替换行值?
问题描述
我有一堆这样的专栏
District
________
State District 1
State District 2
State District 3
4th State House District
5th State House District
State District 6
...
State District 17
我想转换其中的每一个,所以它只包含整数值:
District
________
1
2
3
4
5
6
...
17
熊猫如何做到这一点?Pandas 甚至可以实现这一点,还是我必须使用 SQL 或某些数据库语言来执行这种转换?
解决方案
一个简单的解决方案str.replace
是去掉所有非数字字符:
df['District'] = df['District'].str.replace('\D', '')
推荐阅读
- php - 自从服务器移动后,iOS 上的图像 url 请求,但 android 很好
- android - 由于密码重音,签名 APK 失败?
- java - 在 2D 数组中在 0 和 1 之间循环的 for 循环
- bash - 在命令行上获取 git-commit-ids
- c# - 生成列表
在类似参数中使用索引 - postgresql - Psql 格式化数字,使其始终保留两位小数
- excel - 仅在 30k 行上运行但不再运行的宏
- javascript - 旋转html插入符号不会使用css从中心旋转
- codenameone - 桌面版本上的 iOS 主题?
- sql - 在同一个 select 语句中聚合两个值。由于某种原因,第二次聚合的每一行的价值都在下降