Python pandas regex新列nan-但regex tester显示regex有效

Python pandas regex新列nan-但regex tester显示regex有效,python,regex,pandas,dataframe,Python,Regex,Pandas,Dataframe,我有一个来自测试回归失败的错误消息的csv,我正在将它导入pandas数据帧,但我想找到一些与异常相关的子字符串,特别是 我用.csv的内容填充数据框,如下所示: df = pd.read_csv('ErrorMessage3.csv', header=None, sep=',', names=['ErrorMessage']) 我有以下正则表达式和相应的测试字符串(这是错误消息dataframe列中的第一个条目),它返回的正是我想要的: teststring =

我有一个来自测试回归失败的错误消息的csv,我正在将它导入pandas数据帧,但我想找到一些与异常相关的子字符串,特别是

我用.csv的内容填充数据框,如下所示:

df = pd.read_csv('ErrorMessage3.csv', header=None, sep=',', 
             names=['ErrorMessage'])
我有以下正则表达式和相应的测试字符串(这是错误消息dataframe列中的第一个条目),它返回的正是我想要的:

teststring = "Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp 
Date Record from Epay Account {DBServer;UserName;Password='', 
DatabaseName='',Year Offset='-10'}> ---> 
System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or 
property cannotbecalled 
on Null values. ---> System.Data.SqlTypes.SqlNullValueException2: Data is Null."

re.findall(r"---> ([^:]+): ", teststring)
但我希望能够将其作为“例外”列添加到我的数据框架中。我认为这会奏效:

df['Exceptions'] = df['ErrorMessage'].str.extract(r"---> ([^:]+): ")
但当我运行它时,我会添加“Exceptions”列,但所有行都是NaN。我验证了我的ErrorMessage是对象类型,并且我使用了一个在线正则表达式测试器来验证我的ErrorMessage条目中至少有一个子集确实包含与我的正则表达式匹配的异常。我也读过一些类似的堆栈溢出问题,但我运气不太好


为什么将正则表达式应用于数据帧会产生nan,但将其应用于单个字符串会返回我想要的结果?

正如@Trenton\u M指出的,extractall会返回一个新的多索引数据帧,因此一种解决方案是使用
groupby
然后连接所有匹配的字符串

teststring1 = """Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp Date Record from Epay Account 
                {DBServer;UserName;Password='', DatabaseName='',Year Offset='-10'}> ---> System.Data.SqlTypes.SqlNullValueException1: 
                Data is Null. This method or property cannotbecalled on Null values. ---> System.Data.SqlTypes.SqlNullValueException2: Data is Null. 
                ---> System.Data.SqlTypes.SqlNullValueException21:  ---> System.Data.SqlTypes.SqlNullValueException22:  ---> System.Data.SqlTypes.SqlNullValueException23: 
                ---> System.Data.SqlTypes.SqlNullValueException24: """
teststring2 = """Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp Date Record from Epay Account 
                {DBServer;UserName;Password='', DatabaseName='',Year Offset='-10'}> ---> System.Data.SqlTypes.SqlNullValueException3: 
                Data is Null. This method or property cannotbecalled on Null values. ---> System.Data.SqlTypes.SqlNullValueException4: Data is Null."""
teststring3 = """Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp Date Record from Epay Account 
                {DBServer;UserName;Password='', DatabaseName='',Year Offset='-10'}> ---> System.Data.SqlTypes.SqlNullValueException5: 
                Data is Null. This method or property cannotbecalled on Null values. ---> System.Data.SqlTypes.SqlNullValueException6: Data is Null."""
teststring4 = """Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp Date Record from Epay Account 
                {DBServer;UserName;Password='', DatabaseName='',Year Offset='-10'}> ---> System.Data.SqlTypes.SqlNullValueException7: 
                Data is Null. This method or property cannotbecalled on Null values. ---> System.Data.SqlTypes.SqlNullValueException8: Data is Null."""
teststring5 = """Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp Date Record from Epay Account 
                {DBServer;UserName;Password='', DatabaseName='',Year Offset='-10'}> ---> System.Data.SqlTypes.SqlNullValueException9: 
                Data is Null. This method or property cannotbecalled on Null values. ---> System.Data.SqlTypes.SqlNullValueException10: Data is Null."""
teststring6 = """Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp Date Record from Epay Account 
                {DBServer;UserName;Password='', DatabaseName='',Year Offset='-10'}> ---> System.Data.SqlTypes.SqlNullValueException11: 
                Data is Null. This method or property cannotbecalled on Null values. ---> System.Data.SqlTypes.SqlNullValueException12: Data is Null."""


values = [[teststring1], [teststring2], [teststring3], [teststring4], [teststring5], [teststring6]]
header = ['ErrorMessage']

df = pd.DataFrame(values, columns=header)

exceptions = df['ErrorMessage'].str.extractall(r"---> ([^:]+): ")
下面是一个简单的演示:

import pandas as pd
import numpy as np
df = pd.DataFrame([""""Step 13 - Iteration 1 Failed: Action: <Update Latest CC Exp 
Date Record from Epay Account {DBServer;UserName;Password='', 
DatabaseName='',Year Offset='-10'}> ---> 1System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or 
property cannotbecalled 
on Null values. ---> 2System.Data.SqlTypes.SqlNullValueException2: Data is Null."""] * 2, columns=['ErrorMessage'])

mulIndexDataFrame = df['ErrorMessage'].str.extractall(r"---> ([^:]+): ")
df['test'] = mulIndexDataFrame.groupby(mulIndexDataFrame.index.get_level_values(0))[0].apply(lambda x: ','.join(x))
print(df)
将熊猫作为pd导入
将numpy作为np导入
df=pd.DataFrame([“”)步骤13-迭代1失败:操作:-->1System.Data.SqlTypes.SqlNullValueException:数据为Null。此方法或
财产不可收回
在空值上。-->2System.Data.SqlTypes.SqlNullValueException2:数据为空。“]*2,列=['ErrorMessage'])
mulIndexDataFrame=df['ErrorMessage'].str.extractall(r“--->([^:]+):”)
df['test']=mulIndexDataFrame.groupby(mulIndexDataFrame.index.get_level_values(0))[0]。应用(lambda x:','.join(x))
打印(df)
输出:

                                        ErrorMessage  \
0  "Step 13 - Iteration 1 Failed: Action: <Update...   
1  "Step 13 - Iteration 1 Failed: Action: <Update...   

                                                test  
0  1System.Data.SqlTypes.SqlNullValueException,2S...  
1  1System.Data.SqlTypes.SqlNullValueException,2S...  
ErrorMessage\

0“步骤13-迭代1失败:操作:尝试
df['Exceptions']=df['ErrorMessage'].str.extractall(r”-->([^:]+)”)。应用(“,”.join)
@WiktorStribiżew相同的结果,我的异常列只包含NaN@Sphinx是的,如果我接受teststring并手动将其分配为dataframe中的一个元素,它就可以工作了。问题是,当通过读取.csv来填充数据帧时,它不起作用。我将首先进行编辑,以显示我是如何读取数据的,这可能会有所帮助。@sphinx不确定我是否遵循了,我针对数据框的“错误消息”列中存在的几个条目测试了正则表达式,并且我用来演示的测试字符串是从该列中的第一个条目复制而来的。这就是这里的关键问题:我知道正则表达式在应用于该字符串时会返回一些内容,但在直接应用于数据帧时它就不起作用了。str.extract只会在字符串@sphinxI中找到第一个匹配项,如下所示:mulIndexDataFrame=df['ErrorMessage'].str.extractall(r“-->([^:]+):”)它创建了一个大小为(0,1)的新数据帧(如预期的那样),它是空的。我想这是预料不到的。我想我通过读取.csv创建的原始数据帧肯定有问题,但我不确定如何排除故障。@如果是,请尝试
mulIndexDataFrame=df['ErrorMessage'].str.extractall(r)([^\n])”
,输出是什么?输出是什么?
                                        ErrorMessage  \
0  "Step 13 - Iteration 1 Failed: Action: <Update...   
1  "Step 13 - Iteration 1 Failed: Action: <Update...   

                                                test  
0  1System.Data.SqlTypes.SqlNullValueException,2S...  
1  1System.Data.SqlTypes.SqlNullValueException,2S...