Search code examples
pythonpandasstringdataframecontains

How to make pandas str.contains (don't have to be starts with) query 2 letter consecutively


Here's my dataset

Id sitename
1  BERASAMA
2  LTE_RS_HARAPAN
3  RSA_IBU
4  OFFICE

Here's my code

filter = df[df['sitename'].str.contains("RS")]

What I Expect

Id sitename
2  LTE_RS_HARAPAN
3  RSA_IBU

Reality

Id sitename
1  BERASAMA
2  LTE_RS_HARAPAN
3  RSA_IBU

I Know that "BERASAMA" is contain R and S and not consecutively


Solution

  • I think you're looking for str.startswith:

    out = df[df['sitename'].str.startswith("RS")]
    

    Output:

       Id    sitename
    1   2     RS_HARAPAN
    2   3     RSA_IBU