首页 > 解决方案 > 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.

标签: regexpython-3.xpandasjupyter-notebook

解决方案


使用.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]'


推荐阅读