[英]Check if string column last characters are numbers in Pandas
I have this dataframe: 我有这个数据帧:
Code Mark
0 Abd 43212312312
1 Charles de Gaulle
2 Carlitos 4132411
3 Antonio
If the last 5 characters of the string in the Code column are numbers, I want that 'Mark' is 'A', so it will look like this: 如果Code列中字符串的最后5个字符是数字,我希望'Mark'是'A',所以它看起来像这样:
Code Mark
0 Abd 43212312312 A
1 Charles de Gaulle
2 Carlitos 4132411 A
3 Antonio
I'm trying to use isnumeric but I'm constantly getting AttributeError: 'Series' object has no attribute 'isnumeric'
我正在尝试使用isnumeric,但我经常遇到AttributeError:
'Series' object has no attribute 'isnumeric'
Can someone help on that? 有人可以帮忙吗?
You are close. 你很亲密 The trick is to use the
.str
accessor via pd.Series.str.isnumeric
. 诀窍是通过
pd.Series.str.isnumeric
使用.str
访问pd.Series.str.isnumeric
。
Then map to 'A' or an empty string via pd.Series.map
: 然后通过
pd.Series.map
映射到'A'或空字符串:
df['Mark'] = df['Code'].str[-5:]\
.str.isnumeric()\
.map({True: 'A', False: ''})
print(df)
Code Mark
0 Abd43212312312 A
1 CharlesdeGaulle
2 Carlitos4132411 A
3 Antonio
Using pd.Series.str.match
, you can use 使用
pd.Series.str.match
,您可以使用
import numpy as np
df['Mark'] = np.where(df.Code.str.match(r'.*?\d{5}$'), 'A', '')
Note that '.*?
请注意
'.*?
' is a non-greedy regex match, '\\d{5}'
checks for 5 digits, and '$'
matches a string end. '是非贪婪的正则表达式匹配,
'\\d{5}'
检查5位数, '$'
匹配字符串结尾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.