无法对与pandas/python中的正则表达式匹配的行求和

无法对与pandas/python中的正则表达式匹配的行求和,python,regex,pandas,Python,Regex,Pandas,我可以在一个数据帧中找到一列中不遵循模式的行数,但不能找到遵循完全相同模式的行数 这项工作: df.report_date.apply(lambda x:(不重新匹配(r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}',x)).sum() 这不会:删除“not”不会告诉我匹配了多少行,但会引发TypeError。你知道为什么会这样吗? df.report_date.apply(lambda x:(re.match(r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}

我可以在一个数据帧中找到一列中不遵循模式的行数,但不能找到遵循完全相同模式的行数

这项工作:

df.report_date.apply(lambda x:(不重新匹配(r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}',x)).sum()

这不会:删除“not”不会告诉我匹配了多少行,但会引发TypeError。你知道为什么会这样吗?

df.report_date.apply(lambda x:(re.match(r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}',x))).sum()



regex = r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}'
df.groupby(df.report_date.str.match(regex)).size()

report_date
False    2
True     3
dtype: int64

问题是match函数在匹配时不返回True,而是返回一个匹配对象。熊猫无法添加此匹配对象,因为它不是整数值。使用“not”时获得和的原因是,它返回一个布尔值True,可以对真值求和并返回一个数字

您能否添加一些数据,即您使用的输入和您的预期结果?!(r’[0-9[0-9[0-9[0-9[0-9[0-9[0-9[1,2{[1,2}}-[0-9[1,2 2{[1,2{[0-9[0-9[0-9[0-9[0-9[0-9[0-9[0-9[1,2[0[0-9[1,2[1,2[1,2]5[1,2]5[1,2[1,2[1,2]5[1[1,2[1[1,2[1,2]7[1,2]5[1,2]5[1[1,2[1,2[1[1,2[1,2]5[1,2[1[1,2]5[1,2[1,2[1,2[1,2]5[1,2]5[1,2]7 7 7[1,2]5]5{1,2}-[0-9]{1,2}',x)).sum()这两种情况都适用。
regex = r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}'
print('does match:    {}\ndoesn\'t match: {}'.format(
        df.report_date.str.match(regex).sum(),
        df.report_date.str.match(regex).__neg__().sum()
    ))

does match:    3
doesn't match: 2
regex = r'[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}'
df.groupby(df.report_date.str.match(regex)).size()

report_date
False    2
True     3
dtype: int64