我有以下Python代码(我希望文本字段中特定数字的第一个匹配):
import numpy as np
import pandas
data = {'A': [1, 2, 3], 'B': ['bla 4044 bla', 'bla 5022 bla', 'bla 6045 bla']}
df = pandas.DataFrame(data)
def fun_subjectnr(column):
column = str(column)
subjectnr = re.search(r"(\b[4][0-1][0-9][0-9]\b)",column)
subjectnr1 = re.search(r"(\b[2-3|6-8][0-9][0-9][0-5]\b)",column)
subjectnr = np.where(subjectnr == "" and subjectnr1 != "", subjectnr1,
subjectnr)
return subjectnr1
df['C'] = df['B'].apply(fun_subjectnr)
通缉输出:
A B C
1 bla 4044 bla 4044
2 bla 5022 bla None
3 bla 6045 bla 6045
它似乎不起作用。当我向正则表达式代码添加[0]时,它会给出错误...(subjectnr = re.search(r“(\ b [4] [0-1] [0-9] [0-9] \ b)”,列)[0])
谁知道该怎么办?提前致谢!