这可行(使用 Pandas 12 开发版)
table2=table[table['SUBDIVISION'] =='INVERNESS']
然后我意识到我需要使用“开始于”来选择字段,因为我错过了一堆。 因此,根据 Pandas 文档,我尽我所能地尝试了
criteria = table['SUBDIVISION'].map(lambda x: x.startswith('INVERNESS'))
table2 = table[criteria]
得到 AttributeError: 'float' object has no attribute 'startswith'
所以我尝试了另一种结果相同的语法
table[[x.startswith('INVERNESS') for x in table['SUBDIVISION']]]
引用 http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing 第 4 节:Series 的列表推导和 map 方法也可用于生成更复杂的标准:
我错过了什么?
最佳答案
您可以使用 str.startswith
DataFrame 方法提供更一致的结果:
In [11]: s = pd.Series(['a', 'ab', 'c', 11, np.nan])
In [12]: s
Out[12]:
0 a
1 ab
2 c
3 11
4 NaN
dtype: object
In [13]: s.str.startswith('a', na=False)
Out[13]:
0 True
1 True
2 False
3 False
4 False
dtype: bool
并且 bool 索引可以正常工作(我更喜欢使用 loc
,但不使用它也一样):
In [14]: s.loc[s.str.startswith('a', na=False)]
Out[14]:
0 a
1 ab
dtype: object
.
看起来系列/列中至少有一个元素是 float ,它没有startswith方法,因此出现AttributeError,列表推导应该引发相同的错误......
关于python - Pandas 使用startswith从Dataframe中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17957890/