- 用法:- Index.searchsorted(value, side='left', sorter=None)
- 查找應插入元素以保持順序的索引。 - 將索引查找到排序索引 - self中,這樣,如果在索引之前插入- value中的相應元素,則將保留- self的順序。- 注意 - 索引必須單調排序,否則可能會返回錯誤的位置。 Pandas 不會為您檢查此項。 - value:array-like 或標量
- 要插入 - self的值。
- side:{‘left’, ‘right’},可選
- 如果‘left’,則給出找到的第一個合適位置的索引。如果‘right’,返回最後一個這樣的索引。如果沒有合適的索引,則返回 0 或 N(其中 N 是 - self的長度)。
- sorter:一維array-like,可選
- 可選的整數索引數組,將 - self排序為升序。它們通常是- np.argsort的結果。
 
- int 或 int 數組
- 與 - value具有相同形狀的插入點的標量或數組。
 
 - 參數:- 返回:- 注意:- 二分查找用於查找所需的插入點。 - 例子:- >>> ser = pd.Series([1, 2, 3]) >>> ser 0 1 1 2 2 3 dtype:int64- >>> ser.searchsorted(4) 3- >>> ser.searchsorted([0, 4]) array([0, 3])- >>> ser.searchsorted([1, 3], side='left') array([0, 2])- >>> ser.searchsorted([1, 3], side='right') array([1, 3])- >>> ser = pd.Series(pd.to_datetime(['3/11/2000', '3/12/2000', '3/13/2000'])) >>> ser 0 2000-03-11 1 2000-03-12 2 2000-03-13 dtype:datetime64[ns]- >>> ser.searchsorted('3/14/2000') 3- >>> ser = pd.Categorical( ... ['apple', 'bread', 'bread', 'cheese', 'milk'], ordered=True ... ) >>> ser ['apple', 'bread', 'bread', 'cheese', 'milk'] Categories (4, object):['apple' < 'bread' < 'cheese' < 'milk']- >>> ser.searchsorted('bread') 1- >>> ser.searchsorted(['bread'], side='right') array([3])- 如果值不是單調排序的,則可能會返回錯誤的位置: - >>> ser = pd.Series([2, 1, 3]) >>> ser 0 2 1 1 2 3 dtype:int64- >>> ser.searchsorted(1) 0 # wrong result, correct would be 1
相關用法
- Python pandas.Index.set_names用法及代碼示例
- Python pandas.Index.str用法及代碼示例
- Python pandas.Index.slice_indexer用法及代碼示例
- Python pandas.Index.shift用法及代碼示例
- Python pandas.Index.symmetric_difference用法及代碼示例
- Python pandas.Index.sort_values用法及代碼示例
- Python pandas.Index.slice_locs用法及代碼示例
- Python pandas.Index.value_counts用法及代碼示例
- Python pandas.Index.argmin用法及代碼示例
- Python pandas.Index.is_categorical用法及代碼示例
- Python pandas.Index.to_series用法及代碼示例
- Python pandas.Index.to_numpy用法及代碼示例
- Python pandas.Index.is_object用法及代碼示例
- Python pandas.Index.is_interval用法及代碼示例
- Python pandas.Index.notnull用法及代碼示例
- Python pandas.Index.equals用法及代碼示例
- Python pandas.Index.duplicated用法及代碼示例
- Python pandas.Index.is_monotonic_increasing用法及代碼示例
- Python pandas.Index.min用法及代碼示例
- Python pandas.Index.is_monotonic_decreasing用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.Index.searchsorted。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
