你可以使用
boolean indexing
检查是否
type
是
string
:
数据帧
:
df = pd.DataFrame([[float(1.2), float(1.3), str("63478"), float(1.1)],
[float(1.2), float(1.3), float(1.1), str("63478")]]).T
print (df)
0 1
0 1.2 1.2
1 1.3 1.3
2 63478 1.1
3 1.1 63478
print (df.applymap(lambda x: isinstance(x, str)))
0 1
0 False False
1 False False
2 True False
3 False True
print (df.applymap(lambda x: isinstance(x, str)).any(axis=1))
0 False
1 False
2 True
3 True
dtype: bool
print (df[~df.applymap(lambda x: isinstance(x, str)).any(axis=1)])
0 1
0 1.2 1.2
1 1.3 1.3
:
s = pd.Series([float(1.2), float(1.3), str("63478"), float(1.1)])
print (s)
0 1.2
1 1.3
2 63478
3 1.1
dtype: object
print (s.apply(lambda x: isinstance(x, str)))
0 False
1 False
2 True
3 False
dtype: bool
print (s[~s.apply(lambda x: isinstance(x, str))])
0 1.2
1 1.3
3 1.1
dtype: object