Python – Drop rows if value in a specific column is not an integer in pandas dataframe

pandaspython

If I have a dataframe and want to drop any rows where the value in one column is not an integer how would I do this?

The alternative is to drop rows if value is not within a range 0-2 but since I am not sure how to do either of them I was hoping someonelse might.

Here is what I tried but it didn't work not sure why:

df = df[(df['entrytype'] != 0) | (df['entrytype'] !=1) | (df['entrytype'] != 2)].all(1)

Best Solution

There are 2 approaches I propose:

In [212]:

df = pd.DataFrame({'entrytype':[0,1,np.NaN, 'asdas',2]})
df
Out[212]:
  entrytype
0         0
1         1
2       NaN
3     asdas
4         2

If the range of values is as restricted as you say then using isin will be the fastest method:

In [216]:

df[df['entrytype'].isin([0,1,2])]
Out[216]:
  entrytype
0         0
1         1
4         2

Otherwise we could cast to a str and then call .isdigit()

In [215]:

df[df['entrytype'].apply(lambda x: str(x).isdigit())]
Out[215]:
  entrytype
0         0
1         1
4         2