I have an input DataFrame which I want to modify one of its 'Spcx' columns, for this I have defined an ascending sorted list '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')]
What I am looking for is that each Example ['Spcx'] is compared with each interval of 'Spaces' and take the value on the left, for example:
0 - -> Spaces [0 - 0.100] - -> 0
0.10 - -> Spaces [0.100 - 0.125] - -> 0.100
0.14 - -> Spaces [0.125 - 0.150] - -> 0.125
It should stay like this:
Spcx
0
0
0.125
0.1
0.1
0.1
0.125
0.15
0.225
0.2
0.125
0.125