且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果特定列中的值不是pandas数据框中的整数,则删除行

更新时间:2021-08-21 09:01:34

我建议使用2种方法:

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

如果值的范围受到您的限制,则使用isin将是最快的方法:

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

否则我们可以强制转换为str,然后调用.isdigit()

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