美文网首页
pandas 获取指定元素所在位置

pandas 获取指定元素所在位置

作者: 天涯西剑 | 来源:发表于2022-01-09 16:26 被阅读0次

    为了获取所在位置而不是所在索引,对于index推荐使用两种方式index.get_loc(ele)index.to_list().index(ele),第一种语句更短,第二种更快

    In [1]: import pandas as pd                                                
    
    In [2]: import numpy as np                                                 
    
    In [3]: data = [406400, 203200, 101600,  76100,  50800,  25400,  19050,  12700, 
       ...:          9500,   6700,   4750,   3350,   2360,   1700,   1180,    850, 
       ...:           600,    425,    300,    212,    150,    106,     75,     53, 
       ...:            38]                                                                               
    
    In [4]: myseries = pd.Series(data, index=range(1,26))                                                
    
    In [5]: myseries[21]                                                                                 
    Out[5]: 150
    
    In [7]: %timeit myseries[myseries == 150].index[0]                                                   
    416 µs ± 5.05 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    In [8]: %timeit myseries[myseries == 150].first_valid_index()                                        
    585 µs ± 32.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    In [9]: %timeit myseries.where(myseries == 150).first_valid_index()                                  
    652 µs ± 23.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    In [10]: %timeit myseries.index[np.where(myseries == 150)[0][0]]                                     
    195 µs ± 1.18 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    In [11]: %timeit pd.Series(myseries.index, index=myseries)[150]                 
    178 µs ± 9.35 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    In [12]: %timeit myseries.index[pd.Index(myseries).get_loc(150)]                                    
    77.4 µs ± 1.41 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    In [13]: %timeit myseries.index[list(myseries).index(150)]
    12.7 µs ± 42.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    
    In [14]: %timeit myseries.index[myseries.tolist().index(150)]                   
    9.46 µs ± 19.2 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    相关文章

      网友评论

          本文标题:pandas 获取指定元素所在位置

          本文链接:https://www.haomeiwen.com/subject/wfiucrtx.html