【问题标题】:Correct use of numpy searchsorted routine正确使用 numpy searchsorted 例程
【发布时间】:2021-10-25 18:09:33
【问题描述】:

我有一个输入 DataFrame,我想修改它的“Spcx”列之一,为此我定义了一个升序排序列表“Spaces”

import pandas as pd
import numpy as np
if __name__ == "__main__":

    Example = [[   0],
               [   0],
               [0.14],
               [0.10],
               [0.10],
               [0.10],
               [0.13],
               [0.16],
               [0.24],
               [0.21],
               [0.14],
               [0.14]]
            
    Example = pd.DataFrame(data = Example, columns = ['Spcx'])
    
    Spaces = [0, 0.100, 0.125, 0.150, 0.175, 0.200, 0.225, 0.250, 0.275, 0.300]

    Spaces = np.array(Spaces)  # convert to numpy array
    Example["Spcx"] = Spaces[np.searchsorted(Spaces, Example["Spcx"], side = 'left')]

我正在寻找的是每个 Example ['Spcx'] 与'Spaces'的每个区间进行比较并取左侧的值,例如:

0 - -> 空格 [0 - 0.100] - -> 0

0.10 - -> 空格 [0.100 - 0.125] - -> 0.100

0.14 - -> 空格 [0.125 - 0.150] - -> 0.125

应该是这样的:

Spcx    
0       
0       
0.125   
0.1     
0.1        
0.1      
0.125   
0.15
0.225      
0.2        
0.125   
0.125   

【问题讨论】:

    标签: python python-3.x pandas numpy


    【解决方案1】:

    一种方法,就是使用 side='right' 并减去 1:

    Spaces = np.array(Spaces)  # convert to numpy array
    Example["Spcx"] = Spaces[np.searchsorted(Spaces, Example["Spcx"], side='right') - 1]
    print(Example)
    

    输出

         Spcx
    0   0.000
    1   0.000
    2   0.125
    3   0.100
    4   0.100
    5   0.100
    6   0.125
    7   0.150
    8   0.225
    9   0.200
    10  0.125
    11  0.125
    

    np.searchsorted 的文档中,假设a 是排序数组,它将返回:

    side returned index i satisfies
    left a[i-1] < v <= a[i]
    right a[i-1] <= v < a[i]

    基本上"right" 将返回i,这样i - 1 对应于小于或等于正在搜索的最后一个值。

    【讨论】:

      【解决方案2】:

      你想要 numpy digitize 函数:

      import pandas as pd
      import numpy as np
      if __name__ == "__main__":
      
          Example = [[   0],
                     [   0],
                     [0.14],
                     [0.10],
                     [0.10],
                     [0.10],
                     [0.13],
                     [0.16],
                     [0.24],
                     [0.21],
                     [0.14],
                     [0.14]]
                  
          Example = pd.DataFrame(data = Example, columns = ['Spcx'])
          
          Spaces = [0, 0.100, 0.125, 0.150, 0.175, 0.200, 0.225, 0.250, 0.275, 0.300]
      
          Spaces = np.array(Spaces)  # convert to numpy array
          Example["Spcx"] = Spaces[np.digitize(Example["Spcx"],Spaces)-1]
          print(Example)
      

      输出:

           Spcx
      0   0.000
      1   0.000
      2   0.125
      3   0.100
      4   0.100
      5   0.100
      6   0.125
      7   0.150
      8   0.225
      9   0.200
      10  0.125
      11  0.125
      

      【讨论】:

        【解决方案3】:

        您可以尝试pd.cutright 选项:

        Spaces[pd.cut(Example['Spcx'], Spaces, right=False, labels=False)]
        

        输出:

        array([0.   , 0.   , 0.125, 0.1  , 0.1  , 0.1  , 0.125, 0.15 , 0.225,
               0.2  , 0.125, 0.125])
        

        【讨论】:

          猜你喜欢
          • 2014-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多