筛选熊猫系列中的字符串

时间:2018-09-14 16:09:30

标签: python pandas

有没有比这更好的方法来过滤熊猫系列中的字符串了?

这是我想出的:

df = pd.DataFrame({'a': [1,2,3,4,'cat','hat','mat'], 'b': [1,2,3,4,5,6,7] })

原文:

a   b
0   1   1
1   2   2
2   3   3
3   4   4
4   cat 5
5   hat 6
6   mat 7

df = df[df['a'].apply(lambda x: isinstance(x, str))]

结果:

a   b
4   cat 5
5   hat 6
6   mat 7

但是,此语法似乎很冗长。有形式吗? :

df[df['a'].is_type(str)] 

编辑:我对检查类型而不是内容感兴趣。因此,例如,我想包含'12345'而排除12345

2 个答案:

答案 0 :(得分:4)

我会推荐to_numeric

df[pd.to_numeric(df.a,errors='coerce').isna()]
Out[246]: 
     a  b
4  cat  5
5  hat  6
6  mat  7

如果您认为评论中提到的情况为零

df[df.a.map(lambda x : type(x).__name__)=='str']
Out[257]: 
     a  b
4  cat  5
5  hat  6
6  mat  7

答案 1 :(得分:1)

您也可以尝试:

df[df.a.str.isalpha() == True]

出局:

     a  b
4  cat  5
5  hat  6
6  mat  7
相关问题