Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 括号后数字的正则表达式(_Python_Regex - Fatal编程技术网

Python 括号后数字的正则表达式(

Python 括号后数字的正则表达式(,python,regex,Python,Regex,我有一个列,其值如下: 4.3生效 44 39有效 我能够用它为第一个数字得到一个新的列 df['new'] = df['column'].str.extract('(\d+)') 如何为第二个数字获取一个新列?3、39等。您只需获取该行,将其转换为字符串,拆分它,然后访问所需的数字,例如: row = '4 (3 in force)' row.split(' ') # This returns ['4', '(3', 'in', 'force)'] row.split(' ')[1] # T

我有一个列,其值如下:

4.3生效 44 39有效 我能够用它为第一个数字得到一个新的列

df['new'] = df['column'].str.extract('(\d+)')

如何为第二个数字获取一个新列?3、39等。

您只需获取该行,将其转换为字符串,拆分它,然后访问所需的数字,例如:

row = '4 (3 in force)'
row.split(' ') # This returns ['4', '(3', 'in', 'force)']
row.split(' ')[1] # This returns '(3'
row.split(' ')[1][1:] # And this returns all numbers after the bracket, so '3'

具体回答您问题的一种方法是使用lookbehind正则表达式,它基本上表示另一个数字后面的第一个数字、一个空格和一个括号:

df['new'] = df['column'].str.extract('(?<=\d+\s\()\d+')

如果你从一个字符串中提取多个部分,你可以考虑合并两个并使用正则表达式中的组来访问所需的部分。

两个格式之间是否会发生变化?这会得到预期的结果,但不使用正则表达式,这是OP询问问题的标记所要求的。