[英]python regex find lowercase followed by capital letter
I feel like I have to apologize in advance for this one, but I've searched for answers and they seem to tell me what I'm doing is correct.我觉得我必须为此提前道歉,但我已经搜索了答案,他们似乎告诉我我所做的是正确的。
I'm trying to set a DataFrame column to True if another column has instances of a lowercase letter immediately followed by an uppercase letter.如果另一列有一个小写字母紧跟一个大写字母的实例,我正在尝试将 DataFrame 列设置为 True。
What I tried was this:我试过的是:
cities['multiteam'] = cities['team'].apply(lambda x: pd.notna(re.search(r'[A][a]',x)))
That's setting all the results to False, so I figured maybe I was doing something wrong with my lambda function and I made the following to debug just the re.search() part:这将所有结果设置为 False,所以我想我的 lambda function 可能做错了,我做了以下内容来调试 re.search() 部分:
cities['multiteam'] = pd.notna(re.search(r'[a][A]','OneTwo'))
That's also setting all the results to False.这也将所有结果设置为 False。 And there I'm stuck.我被困在那里了。
The following code is useful only to look for a letter 'A' followed by the lower case 'a'.以下代码仅适用于查找后跟小写字母“a”的字母“A”。
cities['multiteam'] = cities['team'].apply(lambda t: pd.notna(re.search(r'[A][a]',t)))
You may need to change it if you want to check it for all letters.如果您想检查所有字母,您可能需要更改它。 Maybe replace that line with something like this:也许用这样的东西替换那行:
cities['multiteam'] = cities['team'].apply(lambda t: pd.notna(re.search(r'[A-Z][a-z]',t)))
You should never to apologise about asking questions.你永远不应该为提问而道歉。 Using apply
is quite slow, try and use the str.contains
which can accept a regex pattern.使用apply
非常慢,请尝试使用可以接受正则表达式模式的str.contains
。
cities.assign(multiteam=cities.team.str.contains('[a-z][A-Z]'))
The assign
above is pandas new recommend way of assigning columns.上面的assign
是pandas新推荐的列赋值方式。
The str.contains
works with regex and fixed strings, much faster than apply
. str.contains
适用于正则表达式和固定字符串,比apply
快得多。
The regex pattern above says a range of az
followed by AZ
.上面的正则表达式模式表示一系列az
后跟AZ
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.