python - 什么 Series 方法取代了 searchsorted?

标签 python pandas

在他的视频 [使用 Pandas 在 Python 中进行数据分析] ( http://youtu.be/w26x-z-BdWQ?t=2h14s ),Wes McKinney 展示了一个名为 searchsorted() 的序列方法,它给定一个值,返回序列穿过该值的索引。看来这个功能不再可用了,有别的东西代替了吗?

最佳答案

我相信这是由于 Pandas 0.13.0 中发生的重构,其中 Pandas Series 现在子类 NDFrame 而不是 ndarray 参见 this :

In [33]:

import pandas as pd
import numpy as np
df = pd.DataFrame({'a':arange(10)})
df
Out[33]:

   a
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9

[10 行 x 1 列]

[10 rows x 3 columns]
In [28]:

# you now have to call `.values` to return a ndarray 
df.a.values.cumsum().searchsorted(11)
Out[28]:
5

现在比较一下如果我们使用 numpy 数组会发生什么:

In [29]:

temp = np.array(arange(10))

In [32]:

temp.cumsum().searchsorted(11)
Out[32]:
5

关于python - 什么 Series 方法取代了 searchsorted?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21822988/

相关文章:

python - Django where in queryset 在 url 中用逗号分隔 slug

python - 如何将字符添加到 pandas 列中的日期或 str?

python - 将多条推文放入数据框中

python - 通过 python 脚本和 cron 运行 s3cmd 同步

python - Gunicorn + nginx - 忽略 EPIPE

python - 单个 Numpy 类型似乎不保留字节顺序

python - pandas 计算两个零之间不为零的值的数量

python - 检查 Pandas df 中的列不包含某些文本

python - ModuleNotFoundError : No module named 'pandas._libs.tslibs.frequencies'

python - 如何追溯日志错误?