正则计数和从列打印

发布于 2025-02-02 18:19:57 字数 308 浏览 1 评论 0 原文

我试图在列中计算匹配 REGEX 并打印出发现的金额,下面的代码一直给我 0 。我觉得这不是整列的迭代吗?我的代码如下。

import re

pattern = ('/^[A-Z]{1}\d{8}$/i')
numbers = jan_df['Student Number']

iterator = re.finditer(pattern, str(numbers))
count = 0

for match in iterator:
    count+=1
print(count)

I am trying to count matching regex in a column and print out the amount found, the code below keeps giving me 0. I have a feeling it's not iterating through the whole column? My code is as below.

import re

pattern = ('/^[A-Z]{1}\d{8}$/i')
numbers = jan_df['Student Number']

iterator = re.finditer(pattern, str(numbers))
count = 0

for match in iterator:
    count+=1
print(count)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别闹i 2025-02-09 18:19:58

您可以使用

df.loc[df['Student Number'].str.contains(r'^[A-Za-z]\d{8}

,或者,如果您打算使用更具体的正则义务,并且需要使其案例不敏感:

df.loc[df['Student Number'].str.contains(r'^[A-Z]\d{8}

Notes

  • Python中的Regex用字符串文字定义,而不是Regex Lieltals,则不能使用<代码> /.../ i 事,您需要 ... ,标志为选项,或作为inline flags((?i)... ) )
  • {1} 在正则模式中始终是冗余的,请删除
  • series.str.contains 返回true或false,具体取决于是否有匹配。 df.loc [df [col] .str.contains(...),:] 仅返回找到匹配的那些行
  • dataframe.shape.shape 返回数据框架,因此 .shape [0] 返回行数。

相关的帖子

), :].shape[0]

,或者,如果您打算使用更具体的正则义务,并且需要使其案例不敏感:


Notes

  • Python中的Regex用字符串文字定义,而不是Regex Lieltals,则不能使用<代码> /.../ i 事,您需要 ... ,标志为选项,或作为inline flags((?i)... ) )
  • {1} 在正则模式中始终是冗余的,请删除
  • series.str.contains 返回true或false,具体取决于是否有匹配。 df.loc [df [col] .str.contains(...),:] 仅返回找到匹配的那些行
  • dataframe.shape.shape 返回数据框架,因此 .shape [0] 返回行数。

相关的帖子

, case=False), :].shape[0] # or df.loc[df['Student Number'].str.contains(r'(?i)^[A-Z]\d{8}

Notes

  • Python中的Regex用字符串文字定义,而不是Regex Lieltals,则不能使用<代码> /.../ i 事,您需要 ... ,标志为选项,或作为inline flags((?i)... ) )
  • {1} 在正则模式中始终是冗余的,请删除
  • series.str.contains 返回true或false,具体取决于是否有匹配。 df.loc [df [col] .str.contains(...),:] 仅返回找到匹配的那些行
  • dataframe.shape.shape 返回数据框架,因此 .shape [0] 返回行数。

相关的帖子

), :].shape[0]

,或者,如果您打算使用更具体的正则义务,并且需要使其案例不敏感:


Notes

  • Python中的Regex用字符串文字定义,而不是Regex Lieltals,则不能使用<代码> /.../ i 事,您需要 ... ,标志为选项,或作为inline flags((?i)... ) )
  • {1} 在正则模式中始终是冗余的,请删除
  • series.str.contains 返回true或false,具体取决于是否有匹配。 df.loc [df [col] .str.contains(...),:] 仅返回找到匹配的那些行
  • dataframe.shape.shape 返回数据框架,因此 .shape [0] 返回行数。

相关的帖子

), :].shape[0]

Notes

  • Python中的Regex用字符串文字定义,而不是Regex Lieltals,则不能使用<代码> /.../ i 事,您需要 ... ,标志为选项,或作为inline flags((?i)... ) )
  • {1} 在正则模式中始终是冗余的,请删除
  • series.str.contains 返回true或false,具体取决于是否有匹配。 df.loc [df [col] .str.contains(...),:] 仅返回找到匹配的那些行
  • dataframe.shape.shape 返回数据框架,因此 .shape [0] 返回行数。

相关的帖子

), :].shape[0]

,或者,如果您打算使用更具体的正则义务,并且需要使其案例不敏感:


Notes

  • Python中的Regex用字符串文字定义,而不是Regex Lieltals,则不能使用<代码> /.../ i 事,您需要 ... ,标志为选项,或作为inline flags((?i)... ) )
  • {1} 在正则模式中始终是冗余的,请删除
  • series.str.contains 返回true或false,具体取决于是否有匹配。 df.loc [df [col] .str.contains(...),:] 仅返回找到匹配的那些行
  • dataframe.shape.shape 返回数据框架,因此 .shape [0] 返回行数。

相关的帖子

You can use

df.loc[df['Student Number'].str.contains(r'^[A-Za-z]\d{8}

Or, if you plan to use a more specific regex and need to make it case insensitive:

df.loc[df['Student Number'].str.contains(r'^[A-Z]\d{8}

Notes:

  • The regex in Python is defined with string literals, not regex literals, so you cannot use /.../i thing, you need ... with flags as options, or as inline flags ((?i)...)
  • {1} is always redundant in regex patterns, please remove it
  • Series.str.contains returns True or False depending if there is a match. df.loc[df[col].str.contains(...), :] only returns those rows where the match was found
  • Dataframe.shape returns the dimensions of the data frame, so .shape[0] returns the number of rows.

Related SO posts

), :].shape[0]

Or, if you plan to use a more specific regex and need to make it case insensitive:


Notes:

  • The regex in Python is defined with string literals, not regex literals, so you cannot use /.../i thing, you need ... with flags as options, or as inline flags ((?i)...)
  • {1} is always redundant in regex patterns, please remove it
  • Series.str.contains returns True or False depending if there is a match. df.loc[df[col].str.contains(...), :] only returns those rows where the match was found
  • Dataframe.shape returns the dimensions of the data frame, so .shape[0] returns the number of rows.

Related SO posts

, case=False), :].shape[0] # or df.loc[df['Student Number'].str.contains(r'(?i)^[A-Z]\d{8}

Notes:

  • The regex in Python is defined with string literals, not regex literals, so you cannot use /.../i thing, you need ... with flags as options, or as inline flags ((?i)...)
  • {1} is always redundant in regex patterns, please remove it
  • Series.str.contains returns True or False depending if there is a match. df.loc[df[col].str.contains(...), :] only returns those rows where the match was found
  • Dataframe.shape returns the dimensions of the data frame, so .shape[0] returns the number of rows.

Related SO posts

), :].shape[0]

Or, if you plan to use a more specific regex and need to make it case insensitive:


Notes:

  • The regex in Python is defined with string literals, not regex literals, so you cannot use /.../i thing, you need ... with flags as options, or as inline flags ((?i)...)
  • {1} is always redundant in regex patterns, please remove it
  • Series.str.contains returns True or False depending if there is a match. df.loc[df[col].str.contains(...), :] only returns those rows where the match was found
  • Dataframe.shape returns the dimensions of the data frame, so .shape[0] returns the number of rows.

Related SO posts

), :].shape[0]

Notes:

  • The regex in Python is defined with string literals, not regex literals, so you cannot use /.../i thing, you need ... with flags as options, or as inline flags ((?i)...)
  • {1} is always redundant in regex patterns, please remove it
  • Series.str.contains returns True or False depending if there is a match. df.loc[df[col].str.contains(...), :] only returns those rows where the match was found
  • Dataframe.shape returns the dimensions of the data frame, so .shape[0] returns the number of rows.

Related SO posts

), :].shape[0]

Or, if you plan to use a more specific regex and need to make it case insensitive:


Notes:

  • The regex in Python is defined with string literals, not regex literals, so you cannot use /.../i thing, you need ... with flags as options, or as inline flags ((?i)...)
  • {1} is always redundant in regex patterns, please remove it
  • Series.str.contains returns True or False depending if there is a match. df.loc[df[col].str.contains(...), :] only returns those rows where the match was found
  • Dataframe.shape returns the dimensions of the data frame, so .shape[0] returns the number of rows.

Related SO posts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文