查找大于级别的值 \- Python Pandas

新手上路,请多包涵

在时间序列(有序元组)中,找到第一次满足条件的最有效方法是什么?

特别是,确定 pandas 数据框中列的值何时超过 100 的最有效方法是什么?

我希望有一个聪明的矢量化解决方案,而不必使用 df.iterrows()

例如,对于价格或计数数据,当值超过 100 时。即 df[‘col’] > 100。

               price
date
2005-01-01     98
2005-01-02     99
2005-01-03     100
2005-01-04     99
2005-01-05     98
2005-01-06     100
2005-01-07     100
2005-01-08     98

但对于可能非常大的系列。迭代(慢)更好还是有矢量化解决方案?

A df.iterrows() 解决方案可能是:

 for row, ind in df.iterrows():
    if row['col'] > value_to_check:
        breakpoint = row['value_to_record'].loc[ind]
        return breakpoint
return None

但我的问题更多是关于效率(可能是一个可以很好扩展的矢量化解决方案)。

原文由 Jared 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 359
1 个回答

试试这个:“> 99”

 df[df['price'].gt(99)].index[0]

返回 "2" ,第二个索引行。

所有行索引大于 99

 df[df['price'].gt(99)].index
Int64Index([2, 5, 6], dtype='int64')

原文由 Merlin 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题