我有一个包含许多公司特征的数据框。我的问题是,在“设施名称”列中,我发现有些行具有浮点值,因此我想删除它们。为此,我使用了以下似乎不起作用的代码行:
df = df[isinstance(df['Facility Name'], str)]
知道怎么做吗?
你很接近。这应该有效:
python 2.x:
df = df[df['Facility Name'].apply(lambda x: isinstance(x, basestring))]
Python 3.x:
df = df[df['Facility Name'].apply(lambda x: isinstance(x, (str, bytes)))]
下面是一些 Python 3.x 代码,您可以看到它是如何工作的:
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: df = pd.DataFrame({'Facility Name': [1, 0, None, 'Yes', 'No', 'Maybe So', b'what', np.nan, np.inf]})
In [4]: df
Out[4]:
Facility Name
0 1
1 0
2 None
3 Yes
4 No
5 Maybe So
6 b'what'
7 NaN
8 inf
In [5]: df[df['Facility Name'].apply(lambda x: isinstance(x, str))]
Out[5]:
Facility Name
3 Yes
4 No
5 Maybe So
In [6]: df[df['Facility Name'].apply(lambda x: isinstance(x, (str, bytes)))]
Out[6]:
Facility Name
3 Yes
4 No
5 Maybe So
6 b'what'
我是一名优秀的程序员,十分优秀!