在他的视频 [使用 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/