Search code examples
pythonpandasnumeric

Drop rows containing a certain numeric pattern (int64) in pandas


Surprisingly cannot find a simple answer.

I have two columns in a dataframe. Column1 is int64.

Column1  Column2
19970101  400
19970102  300
19980101  200

How to delete rows with 1997 pattern in Column1? It is not a string, so regular expression would not work, I believe.

I guess I can split it into two parts and remove all rows, just having 1997 separately, and then combine again, but would be great to find a more simple solution.

Thanks!


Solution

  • Well:

    df[df['Column1']//10000 != 1997]
    

    Or converting it to string:

    df[df['Column1'].astype(str).str[:4] != '1997']