Filter rows of one column which is alphabet, numbers or hyphen in Pandas

Use str.match to force all characters:

df['check'] = np.where('^[a-zA-Z\d\-]*$'), np.NaN, 'incorrect')

Or str.contains with negation pattern:

df['check'] = np.where('([^a-zA-Z\d\-])'), 'incorrect', np.NaN)


   id    room      check
0   1   A-102        nan
1   2     201        nan
2   3    B309        nan
3   4   C·102  incorrect
4   5  E_1089  incorrect

If you want to update the existing check column, use loc access. For example:

df.loc['([^a-zA-Z\d\-])'), 'check'] = 'incorrect'
# or safer when `NaN` presents
# df.loc['([^a-zA-Z\d\-])') == True, 'check'] = 'incorrect'

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top