简体   繁体   English

Python 搜索字符串包含字符

[英]Python search string contains characters

I have a data below:我有以下数据:

col1      
086945159
549615853
589ac2546
GED456231
F56hy8W12

I want to find whether col has non-numeric value and return.我想查找col是否具有非数字值并返回。

col1         col2 
086945159    086945159
549615853    549615853
589ac2546    Nan
GED456231    Nan
F56hy8W12    Nan
111111111    Nan
222222222    Nan

I used re.search(r'[^0-9]+', str) to find.我用re.search(r'[^0-9]+', str)来查找。 However, how can I use this in apply() since if value in col has the same number, like 11111111 and 222222222 , this should return Nan .但是,我如何在apply()中使用它,因为如果col中的值具有相同的数字,例如11111111222222222 ,这应该返回Nan

You can use mask with conditional pattern:您可以使用带有条件模式的mask

# first part to match any non-digit
# second part to match identical characters
df['col2'] = df.col1.mask(df.col1.str.contains(r'\D|^(.)\1*$'))

Output: Output:

        col1       col2
0  086945159  086945159
1  549615853  549615853
2  589ac2546        NaN
3  GED456231        NaN
4  F56hy8W12        NaN
5  111111111        NaN
6  222222222        NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM