regex - How do you apply regex in pandas dataframe to extract all values after the first colon (not the second colon)?
问题描述
I have a table where column A has values in '12:30:45' format. I want to create a column B where I only get the digits after the first colon of column A.
How do you use regex on python to extract only the digits after the first colon so that in the end, we get ':30:45'?
I've seen regex for extracting numbers, strings, split values between spaces, but couldn't find the one for extracting after colon.
I'm new to regex and any suggestions would be greatly appreciated.
解决方案
使用.str.split
将拆分次数限制为n=1
.
print(df)
time
0 12:30:45
1 12:30
2 12312
3 1:123:123123:123123
df.time.str.split(':', n=1).str[1]
#0 30:45
#1 30
#2 NaN
#3 123:123123:123123
#Name: time, dtype: object
如果你真的需要前导分号':'+df.time.str.split(':', n=1).str[1]'
。
推荐阅读
- asp.net-mvc - 如何在按钮单击 MVC 时将数据表绑定到 jqGrid
- vba - 可以使用 VBA 操作报表节标题中的文本标签吗?(MS-访问)
- html - 智能手机的横向利润率远高于 PC
- java - android中的图形库或sdk
- java - Hibernate - 从我的示例表中获取错误的实体。看起来表格列被“交换”了
- php - PHP 内部 - 使用什么错误记录功能?
- python - 斯坦福 CoreNLP POS 标记法文
- node.js - VSTS 与 gulp v.4.0.0
- .htaccess - 服务器升级 PHP 和 SQL 后插入数据库的问题
- database - 循环遍历行,复制单元格,然后在新循环中移回原始工作表