用pandas比较DataFrame中的当前行和下一行



我有一个名为"DataExample"和一个名为"normalsizes"的升序排序列表。

import pandas as pd
if __name__ == "__main__":
DataExample = [[0.6,  0.36 ,0.00],
[0.6,  0.36 ,0.00],
[0.9,  0.81 ,0.85],
[0.8,  0.64 ,0.91],
[1.0,  1.00 ,0.92],
[1.0,  1.00 ,0.95],
[0.9,  0.81 ,0.97],
[1.2,  1.44 ,0.97],
[1.0,  1.00 ,0.97],
[1.0,  1.00 ,0.99],
[1.2,  1.44 ,0.99],
[1.1,  1.21 ,0.99]]

DataExample = pd.DataFrame(data = DataExample, columns = ['Lx', 'A', 'Ratio'])

normalsizes = [0, 0.75, 1, 1.25, 1.5, 1.75 ,2, 2.25, 2.4, 2.5, 2.75, 3, 
3.25, 3.5, 3.75, 4, 4.25, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6]

#    for i in example.index:
#        
#        numb = example['Lx'][i]

我要找的是每个"DataExample [' Lx ']"被分析并位于正常大小的范围内,例如:

For DataExample [' Lx '] [0] = 0.6 ----->则区间为[0,0.75]----->0.6比;0和0.6 <= 0.75 ----->我取这个区间的最大值,也就是0.75。

我应该有以下结果:

Lx      A       Ratio
1       0.36    0
1       0.36    0
1       0.81    0.85
1       0.64    0.91
1.25       1    0.92
1.25       1    0.95
1       0.81    0.97
1.25    1.44    0.97
1.25       1    0.97
1.25       1    0.99
1.25    1.44    0.99
1.25    1.21    0.99

numpy。搜索排序会得到你想要的

import numpy as np
normalsizes = np.array(normalsizes)  # convert to numpy array
DataExample["Lx"] = normalsizes[np.searchsorted(normalsizes, DataExample["Lx"])]

相关内容

  • 没有找到相关文章

最新更新