eleanora eleanora - 3 months ago 13
Python Question

Select rows where a particular column has is two characters long

I know how to select rows by value in a particular column. For example:

df.loc[df['column_name'] == some_value]


How do I modify that so the column value is exactly two capital letters. E.g. AB or FZ.

Answer

you can use .str.match() method:

In [55]: df
Out[55]:
     col
0     xy
1    ABC
2     ZS
3  AAAAA
4     XC

In [56]: df.col.str.match(r'^[A-Z]{2}$')
Out[56]:
0    False
1    False
2     True
3    False
4     True
Name: col, dtype: bool

In [57]: df[df.col.str.match(r'^[A-Z]{2}$')]
Out[57]:
  col
2  ZS
4  XC