Home > Enterprise >  Compare current row with next row in a DataFrame with pandas
Compare current row with next row in a DataFrame with pandas

Time:10-25

I have a DataFrame called "DataExample" and an ascending sorted list called "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]

What I am looking for is that each “DataExample [‘ Lx ’]” is analyzed and located within a range of normalsizes, for example:

For DataExample [‘Lx’] [0] = 0.6 -----> then it is between the interval of [0, 0.75] -----> 0.6> 0 and 0.6 <= 0.75 -----> so I take the largest of that interval, that is, 0.75. This for each row.

With this I should have the following result:

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

CodePudding user response:

numpy.searchsorted will get you what you want

import numpy as np

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