python pandas 行以一个字母一个数字通配符开头

python pandas row startswith one letter one number wildcard

尝试过滤掉我的数据中的行,我需要匹配第一个字母 N 后跟一个数字,然后删除不符合此条件的行。

我尝试了来自 Whosebug 的多种正则表达式组合,但它们似乎无法正常工作

new = new.loc[new['call_x'].str.startswith("^[N]{1}[0-9]+")]

Example data
N902AG #keep
N917GA #keep
N918PD #keep
N919PD #keep
N930EN #keep
N940CL #keep
N976TR #keep
N98AW #keep
NAX6700 #drop
NAX7019 #drop
NKS1028 #drop
NKS171 #drop
NKS174 #drop
NKS197 #drop

使用pandas.Series.str.contains匹配正则表达式。

df = df.loc[df['a'].str.contains('^N[0-9]+')]

Pandas str.startswith 不接受正则表达式。你想要str.match。 试试这个:

df[df.Example.str.match('^N\d+')]

str.contains 类似,但会在字符串的任何位置查找匹配项,而不仅仅是开头。