设置
df = pd.DataFrame([['a', 'b', 'c', 'd'], ['e', 'f', 1.2, 'g']], columns=list('ABCD'))
print df
A B C D
0 a b c d
1 e f 1.2 g
请注意,您可以查看各个单元格类型。
print type(df.loc[0, 'C']), type(df.loc[1, 'C'])
<type 'str'> <type 'float'>
蒙版和切片
print df.loc[df.C.apply(type) != float]
A B C D
0 a b c d
更通用
print df.loc[df.C.apply(lambda x: not isinstance(x, (float, int)))]
A B C D
0 a b c d
您也可以使用float 来尝试确定它是否可以是浮点数。
def try_float(x):
try:
float(x)
return True
except:
return False
print df.loc[~df.C.apply(try_float)]
A B C D
0 a b c d
这种方法的问题是您将排除可以解释为浮点数的字符串。
比较我提供的几个选项的时间以及 jezrael 的解决方案与小数据帧。
对于具有 500,000 行的数据框:
检查它的类型是否为浮点数似乎是最好的,它后面是数字。如果您需要检查 int 和 float,我会选择 jezrael 的答案。如果您可以避免检查浮动,请使用那个。