python - Pandas 使用startswith从Dataframe中选择

标签 python numpy pandas

这可行(使用 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/

相关文章:

python - 多年的每一天的平均值

python - Bokeh 日期范围 slider

python - 应该将 Python 记录器作为参数传递吗?

python - 如何解决由FFT驱动的微分程序中的移位和缩放错误?

python - 在 scikit-learn 中的数据集上绘制决策树

python - 将 NaN 值替换为 'INF'

python - 我无法安装 pyopenjtalk "Getting requirements to build wheel did not run successfully."

python - 如何在 psycopg2 查询中返回 json?

python - Numpy,基于数组中的数组的新数组

python - 如何从列表文件中随机忽略暂时不存在的名称?