我有一个df
,一个列(acount_no
)包含空字符串,空格字符串和重复的数字字符串作为其值。对于这些值,我想创建一个新列(valid_account_no
),然后将False
设置为其相应的行。同样,如果任何account_no
的长度为 <=
4,则将将False
设置为valid_account_no
。df
看起来像
id account_no valid_account_no
1 False
2 999999 False
3 1234 False
4 123456 True
这是我的代码,
# sets boolean values of column valid account no when account no is spaced or repeated number
df['valid_account_no'] = df['account_no'].str.match(r"b(d)1+b| +")
# if length of any account nos are <= 4 or the account nos are empty
# set values of column valid account no to False
invalid_account_indices = df[(df['account_no'].str.len() <= 4) |
(df['account_no'] == '')].index
df.loc[invalid_account_indices, 'valid_account_no'] = False
我想知道是否有更好的方法来实现这一目标,从某种意义上说,它更加简洁,效率。
如果好的,您的方法本身本身就是与简单的布尔代数,即
m1 = df['account_no'].str.match(r"b(d)1+b| +")
m2 = df['account_no'].str.len()<=4
m3 = df['account_no'] == ''
df['valid_account_no'] = (m1) | (m2 & m3)