Search code examples
pythonregexpandasstartswith

python pandas row startswith one letter one number wildcard


Trying to filter out rows in my data, I need to match the first letter N followed by a number, then drop rows that don't match this criteria.

I've tried multiple regex combinations from stackoverflow but they don't seem to work properly

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

Solution

  • Use pandas.Series.str.contains to match regexp.

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